OSDN Git Service

2011/10/23 13:25:02
authorqwerty2501 <riot313@gmail.com>
Sun, 23 Oct 2011 04:25:02 +0000 (13:25 +0900)
committerqwerty2501 <riot313@gmail.com>
Sun, 23 Oct 2011 04:25:02 +0000 (13:25 +0900)
JSONParser/json.h
JSONParser/json_parser.c

index 1274be1..79f6940 100644 (file)
@@ -112,6 +112,12 @@ typedef JSONVoid (*JSONArrayEndHandler)(JSONChar_P);
 
 
 
+///rslt\82ª\8f\89\8aú\89»\82³\82ê\82Ä\82¢\82È\82¢
+#define JSON_ERR_NOTINIT                                                               ((JSONResult)UINTPTR_MAX)
+
+
+
+
 
 
 
@@ -154,6 +160,12 @@ typedef JSONVoid (*JSONArrayEndHandler)(JSONChar_P);
 
 #define JSONTypeArray                  ((JSONValueType)3)
 
+#define JSONTypeBool                   ((JSONValueType)4)
+
+#define JSONTypeString                 ((JSONValueType)5)
+
+#define JSONTypeDecimal                        ((JSONValueType)6)
+
 
 
 
index c95f538..29bd42a 100644 (file)
@@ -9,7 +9,7 @@ typedef char JSONText;
 typedef struct jsonparser_t{
 
 
-       JSONChar_P strValue;                                                            //\95\8e\9a\97ñ\92l\95Û\8e\9d\97p\97Ì\88æ
+       JSONChar_P value;                                                               //\95\8e\9a\97ñ\92l\95Û\8e\9d\97p\97Ì\88æ
 
        JSONUInt strValueSize;                                                          //\95\8e\9a\97ñ\83T\83C\83Y
 
@@ -67,7 +67,7 @@ static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsont
 ///
 ///utf8\82Å\83I\83u\83W\83F\83N\83g\82ð\89ð\90Í
 ///
-static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,JSONText **endptr);
+static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr);
 
 
 ///
@@ -76,23 +76,41 @@ static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,J
 static JSONResult ParseObjectMemberUTF8(JSONParser_P parser, const JSONText *jsontext,const JSONText **endptr);
 
 
+///
+///utf8\82Å\94z\97ñ\92l\82ð\89ð\90Í
+///
+static JSONResult ParseArrayValueUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr);
 
+///
+///utf8\82Å\94z\97ñ\82ð\89ð\90Í
+///
+static JSONResult ParseArrayUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr);
 
 ///
 ///utf8\82Å\95\8e\9a\97ñ\82ð\89ð\90Í
 ///
-static JSONResult ParseString(JSONChar_P buf,const JSONText *start,const JSONText **endptr);
+static JSONResult ParseStringUTF8(JSONChar_P buf,const JSONText *jsontext,const JSONText **endptr);
+
 
+///
+///utf8\82Å10\90i\90\94\82ð\89ð\90Í
+///
+static JSONResult ParseDecimalUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr);
 
 ///
 ///utf8\82Å\95\8e\9a\97ñ\92l\82ð\89ð\90Í
 ///
-static JSONResult ParseStringValueUTF8(JSONParser_P parser,JSONText *start,const JSONText **endptr);
+static JSONResult ParseStringValueUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr);
+
+///
+///utf8\82Å\83\8a\83e\83\89\83\8b\96¼\82ð\89ð\90Í
+///
+static JSONResult ParseLiteralNameUTF8(JSONText *literalName,const JSONText *jsontext,const JSONText **endptr); 
 
 ///
 ///json\95\8e\9a\97ñ\82Ì\8fI\92[\82ð\8eæ\93¾
 ///
-static JSONText* GetJSONStringEnd(const JSONText *start);
+static JSONText* GetJSONStringEndUTF8(const JSONText *jsontext);
 
 
 ///
@@ -122,7 +140,7 @@ static JSONUInt U8ToU16(JSONChar_P buf,JSONUInt len,const JSONText *start,JSONTe
 
 JSONResult JSONParser_Parse(JSONParser_P parser,const JSONVoid *jsontext,JSONParameter encode){
 
-       JSONResult rslt;
+       JSONResult rslt = JSON_ERR_NOTINIT;
 
        //\83G\83\93\83R\81[\83h\83^\83C\83v\82Ì\94»\92è
        switch(encode){
@@ -131,7 +149,7 @@ JSONResult JSONParser_Parse(JSONParser_P parser,const JSONVoid *jsontext,JSONPar
        case JSON_ENCODE_UTF8:
 
                //utf8\97p\82Ì\89ð\90Í
-               JSONParser_ParseUTF8(parser,(const JSONText*)jsontext);
+               rslt = JSONParser_ParseUTF8(parser,(const JSONText*)jsontext);
 
                break;
 
@@ -151,7 +169,7 @@ JSONResult JSONParser_Parse(JSONParser_P parser,const JSONVoid *jsontext,JSONPar
 
 static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsontext){
 
-       JSONResult rslt = JSON_ERR_OK;
+       JSONResult rslt = JSON_ERR_NOTINIT;
 
        //\83I\83u\83W\83F\83N\83g\96\94\82Í\94z\97ñ\82Ì\8aJ\8en\95\8e\9a\82ð\8c\9f\8dõ
        jsontext = strpbrk(jsontext,"{[");
@@ -188,9 +206,9 @@ static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsont
 }
 
 
-static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,JSONText **endptr){
+static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr){
 
-       JSONResult rslt;
+       JSONResult rslt = JSON_ERR_NOTINIT;
 
        
 
@@ -198,42 +216,66 @@ static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,J
 
        
 
-
        //'{'\82ð\83X\83L\83b\83v
        jsontext++;
+       //\8bó\94\92\82ð\83X\83L\83b\83v
+       jsontext += strspn(jsontext," \r\n\t");
 
-       
+       if(jsontext[0] =='}'){
+               goto end;
+       }
 
        //\83I\83u\83W\83F\83N\83g\83\81\83\93\83o\82ð\89ð\90Í
        do{
-               //\8bó\94\92\82ð\83X\83L\83b\83v
-               jsontext += strspn(jsontext," \r\n\t");
 
+               
+
+               
+               if(jsontext[0] != '"'){
+
+                       goto syntaxerr;
+
+               }
+
+
+               rslt = ParseObjectMemberUTF8(parser,jsontext,&jsontext);
+
+               if(rslt != JSON_ERR_OK){
+                       goto end;
+               }
 
                switch(jsontext[0]){
 
                        //\8bó\83I\83u\83W\83F\83N\83g\82Ì\8fê\8d\87\82Í\8fI\97¹
                case '}':
 
-                       goto end;
-
-                       //name\8aJ\8en\95\8e\9a\82Ì\8fê\8d\87\83I\83u\83W\83F\83N\83g\83\81\83\93\83o\81[\89ð\90Í\8aJ\8en
-               case '"':
+                       //'}'\82ð\83X\83L\83b\83v
+                       jsontext++;
+                       goto success;
 
+               case ',':
 
-                       break;
+                       //','\82ð\83X\83L\83b\83v
+                       jsontext++;
+                       continue;
 
-                       //\82»\82ê\88È\8aO\82Ì\95\8e\9a\82Ì\8fê\8d\87\82Í\8d\\95\83G\83\89\81[
                default:
 
                        goto syntaxerr;
-               
-               
+
+
+
                }
 
+               //\8bó\94\92\82ð\83X\83L\83b\83v
+               jsontext += strspn(jsontext," \r\n\t");
+
+       }while(JSONFalse);
 
+       
 
-       }while(jsontext[0] == ',');
+success:
+       rslt = JSON_ERR_OK;
 
 end:
        (*endptr) = (JSONText*)jsontext;
@@ -252,7 +294,7 @@ syntaxerr:
 static JSONResult ParseObjectMemberUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr){
 
        JSONStringAllocator stringAllocator;                            //\95\8e\9a\97ñ\83A\83\8d\83P\81[\83^
-       JSONResult rslt = JSON_ERR_OK;
+       JSONResult rslt = JSON_ERR_NOTINIT;
        JSONChar_P name;
        JSONText *nameend;                                                                      //name\8fI\92[\95\8e\9a\83|\83C\83\93\83^
        JSONUInt nameLen;                                                                       //name\82Ì\92·\82³
@@ -261,7 +303,7 @@ static JSONResult ParseObjectMemberUTF8(JSONParser_P parser,const JSONText *json
        jsontext++;
 
        //\8fI\92[\95\8e\9a\83|\83C\83\93\83^\82ð\8eæ\93¾
-       nameend = GetJSONStringEnd(jsontext);
+       nameend = GetJSONStringEndUTF8(jsontext);
 
        //\8fI\92[\95\8e\9a\83|\83C\83\93\83^\82ð\8c³\82É\95Ï\8a·\97p\95\8e\9a\97ñ\82Ì\92·\82³\82ð\8eæ\93¾
        nameLen = getLenU8ToU16(jsontext,nameend);
@@ -270,7 +312,7 @@ static JSONResult ParseObjectMemberUTF8(JSONParser_P parser,const JSONText *json
        name = JSONString_alloc(&stringAllocator,nameLen + 1);
 
        //\83I\83u\83W\83F\83N\83g\96¼\82ð\89ð\90Í
-       rslt = ParseString(name,jsontext,&jsontext);
+       rslt = ParseStringUTF8(name,jsontext,&jsontext);
 
        //\90³\8fí\82Å\82Í\82È\82¢\8fê\8d\87\8fI\97¹
        if(rslt != JSON_ERR_OK){
@@ -295,17 +337,46 @@ static JSONResult ParseObjectMemberUTF8(JSONParser_P parser,const JSONText *json
 
                //value\82ª\83I\83u\83W\83F\83N\83g\82Ì\8fê\8d\87
        case '{':
+               parser->objectHandler(JSONTypeObject,name,(JSONParameter)NULL);
+
+               rslt = ParseObjectUTF8(parser,jsontext,&jsontext);
+
+               if(rslt != JSON_ERR_OK){
+                       goto end;
+               }
 
+               parser->objectEndHandler(name);
                break;
 
                //value\82ª\94z\97ñ\82Ì\8fê\8d\87
        case '[':
+               parser->objectHandler(JSONTypeArray,name,(JSONParameter)NULL);
+
+               rslt = ParseArrayUTF8(parser,jsontext,&jsontext);
+
+               if(rslt != JSON_ERR_OK){
+
+                       goto end;
+
+               }
+
+               parser->arrayEndHandler(name);
 
                break;
 
                //value\82ª\95\8e\9a\97ñ\82Ì\8fê\8d\87
        case '"':
 
+               rslt = ParseStringValueUTF8(parser,jsontext,&jsontext);
+
+               if(rslt != JSON_ERR_OK){
+
+                       goto end;
+
+               }
+
+               parser->objectHandler(JSONTypeString,name,(JSONParameter)parser->value);
+
                break;
 
                //value\82ª\90\94\92l\82Ì\8fê\8d\87
@@ -321,13 +392,65 @@ static JSONResult ParseObjectMemberUTF8(JSONParser_P parser,const JSONText *json
        case '8':
        case '9':
 
+               rslt = ParseDecimalUTF8(parser,jsontext,&jsontext);
+
+               if(rslt != JSON_ERR_OK){
+
+                       goto end;
+
+               }
+
+               parser->objectHandler(JSONTypeDecimal,name,(JSONParameter)parser->value);
+
                break;
 
+               //value\82ªtrue\82Ì\8fê\8d\87
+       case 't':
 
+               rslt = ParseLiteralNameUTF8("true",jsontext,&jsontext);
+               if(rslt != JSON_ERR_OK){
+                       goto end;
+               }
+               
+               parser->objectHandler(JSONTypeBool,name,JSONTrue);
+
+               break;
+
+       case 'f':
+               rslt = ParseLiteralNameUTF8("false",jsontext,&jsontext);
+               if(rslt != JSON_ERR_OK){
+                       goto end;
+               }
+
+               parser->objectHandler(JSONTypeBool,name,JSONFalse);
+               break;
+
+       case 'n':
+
+               rslt = ParseLiteralNameUTF8("null",jsontext,&jsontext);
+               if(rslt != JSON_ERR_OK){
+                       goto end;
+               }
+
+               parser->objectHandler(JSONTypeBool,name,JSONFalse);
+
+               break;
+
+               //value\82É\8eg\97p\89Â\94\\82È\95\8e\9a\88È\8aO\82Ì\95\8e\9a\82Ì\8fê\8d\87
+       default:
+
+               goto syntaxerr;
 
 
        }
 
+       rslt = JSON_ERR_OK;
+
+       //\8bó\94\92\82ð\83X\83L\83b\83v
+       jsontext += strspn(jsontext," \r\n\t");
+
+success:
+       rslt = JSON_ERR_OK;
 
 
 end:
@@ -346,6 +469,85 @@ syntaxerr:
 
 
 
+static JSONResult ParseArrayUTF8(JSONParser_P parser,const JSONText *jsontext,const JSONText **endptr){
+
+
+       JSONResult rslt = JSON_ERR_NOTINIT;
+
+       //'['\82ð\83X\83L\83b\83v
+       jsontext++;
+
+       //\8bó\94\92\82ð\83X\83L\83b\83v
+       jsontext += strspn(jsontext," \r\n\t");
+
+       if(jsontext[0] == ']'){
+
+               goto end;
+
+       }
+
+
+               //\94z\97ñ\82ð\89ð\90Í
+       do{
+
+               
+
+               
+       
+
+
+               rslt = ParseArrayValueUTF8(parser,jsontext,&jsontext);
+
+               if(rslt != JSON_ERR_OK){
+                       goto end;
+               }
+
+               switch(jsontext[0]){
+
+                       //\8bó\83I\83u\83W\83F\83N\83g\82Ì\8fê\8d\87\82Í\8fI\97¹
+               case ']':
+
+                       //'}'\82ð\83X\83L\83b\83v
+                       jsontext++;
+                       goto success;
+
+               case ',':
+
+                       //','\82ð\83X\83L\83b\83v
+                       jsontext++;
+                       continue;
+
+               default:
+
+                       goto syntaxerr;
+
+
+
+               }
+
+               //\8bó\94\92\82ð\83X\83L\83b\83v
+               jsontext += strspn(jsontext," \r\n\t");
+
+       }while(JSONFalse);
+
+success:
+       rslt = JSON_ERR_OK;
+
+
+end:
+       (*endptr) = jsontext;
+
+       return rslt;
+
+syntaxerr:
+
+       rslt = JSON_ERR_SYNTAX;
+
+       goto end;
+
+}
+
+
 
 JSONParser_P JSONParser_new(){
 
@@ -353,7 +555,7 @@ JSONParser_P JSONParser_new(){
 
        parser->strValueSize = sizeof(JSONChar) * 1024;
 
-       parser->strValue = (JSONChar_P)malloc(parser->strValueSize);
+       parser->value = (JSONChar_P)malloc(parser->strValueSize);
        parser->objectHandler = defaultObjectEvent;
        parser->arrayHandler = defaultArrayEvent;
        parser->objectEndHandler = defaultObjectEndEvent;
@@ -366,7 +568,7 @@ JSONParser_P JSONParser_new(){
 JSONVoid JSONParser_delete(JSONParser_P *parser){
 
        
-       free((*parser)->strValue);
+       free((*parser)->value);
        free(*parser);
 
        parser = NULL;