OSDN Git Service

2011/10/23 1:26:19
authorqwerty2501 <riot313@gmail.com>
Sat, 22 Oct 2011 16:26:20 +0000 (01:26 +0900)
committerqwerty2501 <riot313@gmail.com>
Sat, 22 Oct 2011 16:26:20 +0000 (01:26 +0900)
JSONParser/JSONParser.vcxproj
JSONParser/json.h
JSONParser/json_parser.c [new file with mode: 0644]

index 067fea9..0ddcdc1 100644 (file)
@@ -75,7 +75,7 @@
     <ClInclude Include="json.h" />
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="json.c" />
+    <ClCompile Include="json_parser.c" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
index 2944b62..1274be1 100644 (file)
@@ -74,30 +74,86 @@ typedef JSONUInt JSONResult;
 ///
 ///\83I\83u\83W\83F\83N\83g\92l\89ð\90Í\8e\9e\83R\81[\83\8b\83o\83b\83N
 ///
-typedef JSONResult (*JSONObjectDataEventHandler)(JSONValueType,JSONChar_P,JSONParameter);
+typedef JSONVoid (*JSONObjectDataEventHandler)(JSONValueType,JSONChar_P,JSONParameter);
 
 ///
 ///\94z\97ñ\92l\89ð\90Í\8e\9e\83R\81[\83\8b\83o\83b\83N
 ///
-typedef JSONResult (*JSONArrayDataEventHandler)(JSONValueType,JSONParameter);
+typedef JSONVoid (*JSONArrayDataEventHandler)(JSONValueType,JSONParameter);
 
+///
+///\83I\83u\83W\83F\83N\83g\8fI\92[\89ð\90Í\8e\9e\83R\81[\83\8b\83o\83b\83N
+///
+typedef JSONVoid (*JSONObjectEndHandler)(JSONChar_P);
+
+///
+///\94z\97ñ\8fI\92[\89ð\90Í\8e\9e\83R\81[\83\8b\83o\83b\83N
+///
+typedef JSONVoid (*JSONArrayEndHandler)(JSONChar_P);
 
 
 //
 //define
 /////////////////////////////////////////////////////////////////////////
 
+//
+//\83G\83\89\81[
+//
+
+///\90³\8fí\8fI\97¹
+#define JSON_ERR_OK                                                                            ((JSONResult)0)
+
+///\8ew\92è\82µ\82½\83G\83\93\83R\81[\83h\83^\83C\83v\82ª\92è\8b`\82³\82ê\82Ä\82¢\82È\82¢
+#define JSON_ERR_ENCODE_UNKNOWN                                                        ((JSONResult)1)
+
+///json\8d\\95\83G\83\89\81[
+#define JSON_ERR_SYNTAX                                                                        ((JSONResult)2)
+
+
+
+
+
+
+
+
+
+
+
+//
+//\83G\83\93\83R\81[\83h\83\82\81[\83h
+//\8c»\8dÝ\82ÍUTF8\82Ì\82Ý
+//
+
+#define JSON_ENCODE_UTF8               ((JSONParameter)0)
 
 
-#define JSON_RESULT_OK                         ((JSONResult)0)
 
 
+//
+//\90^\8bU\92l
+//
+
+///true
+#define JSONTrue                               ((JSONBool)1)
 
 
+///false
+#define JSONFalse                              ((JSONBool)0)
 
 
 
 
+//
+//\92l\83^\83C\83v
+//
+#define JSONTypeRootObject             ((JSONValueType)0)
+
+#define JSONTypeRootArray              ((JSONValueType)1)
+
+#define JSONTypeObject                 ((JSONValueType)2)
+
+#define JSONTypeArray                  ((JSONValueType)3)
+
 
 
 
@@ -142,13 +198,22 @@ extern "C" {
 
 
 
+       ///
+       ///\83I\83u\83W\83F\83N\83g\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+       ///
+       extern JSONVoid JSONParser_setObjectEndEventHandler(JSONParser_P parser,JSONObjectEndHandler handler);
 
 
+       ///
+       ///\94z\97ñ\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+       ///
+       extern JSONVoid JSONParser_setArrayEndEventHandler(JSONParser_P parser,JSONArrayEndHandler handler);
 
 
-
-
-
+       ///
+       ///\89ð\90Í\8aJ\8en
+       ///
+       extern JSONResult JSONParser_Parse(JSONParser_P parser,const JSONVoid *jsontext,JSONParameter encode);
 
 
 
diff --git a/JSONParser/json_parser.c b/JSONParser/json_parser.c
new file mode 100644 (file)
index 0000000..c95f538
--- /dev/null
@@ -0,0 +1,502 @@
+#include "json.h"
+#include <string.h>
+
+typedef char JSONText;
+
+///
+///json\83p\81[\83T\83I\83u\83W\83F\83N\83g
+///
+typedef struct jsonparser_t{
+
+
+       JSONChar_P strValue;                                                            //\95\8e\9a\97ñ\92l\95Û\8e\9d\97p\97Ì\88æ
+
+       JSONUInt strValueSize;                                                          //\95\8e\9a\97ñ\83T\83C\83Y
+
+       JSONDouble decimalValue;                                                        //\90®\90\94\8c^\95Û\8e\9d\97p
+
+       JSONBool boolValue;                                                                     //\90^\8bU\8c^\95Û\8e\9d\97p\97Ì\88æ
+
+       JSONObjectDataEventHandler objectHandler;                       //\83I\83u\83W\83F\83N\83g\83C\83x\83\93\83g\83n\83\93\83h\83\89
+
+       JSONArrayDataEventHandler arrayHandler;                         //\94z\97ñ\83C\83x\83\93\83g\83n\83\93\83h\83\89
+
+       JSONObjectEndHandler objectEndHandler;                          //\83I\83u\83W\83F\83N\83g\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89
+
+       JSONArrayEndHandler arrayEndHandler;                            //\94z\97ñ\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89
+
+
+
+
+}JSONParser;
+
+typedef struct {
+
+       JSONChar buffer[32];                                                            //\83f\83t\83H\83\8b\83g\82Ì\83o\83b\83t\83@
+
+       JSONBool heapFlag;                                                                      //\83q\81[\83v\97Ì\88æ\82ð\8eg\97p\82µ\82Ä\82¢\82é\82©\82Ì\83t\83\89\83O
+
+       JSONChar_P heapBuf;                                                                     //\83q\81[\83v\97Ì\88æ\97p\83|\83C\83\93\83^
+
+}JSONStringAllocator,*JSONStringAllocator_P;
+
+
+//
+//\83f\83t\83H\83\8b\83g\82Ì\83C\83x\83\93\83g\83n\83\93\83h\83\89
+//
+static JSONVoid defaultObjectEvent(JSONValueType valueType,JSONChar_P name,JSONParameter parameter);
+
+static JSONVoid defaultArrayEvent(JSONValueType valueType,JSONParameter parameter);
+
+static JSONVoid defaultObjectEndEvent(JSONChar_P name);
+
+static JSONVoid defaultArrayEndEvent(JSONChar_P name);
+
+
+
+//
+//\83v\83\89\83C\83x\81[\83g\8aÖ\90\94
+//
+
+///
+///jsontext\82ðutf8\82Å\82ð\89ð\90Í
+///
+static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsontext);
+
+
+///
+///utf8\82Å\83I\83u\83W\83F\83N\83g\82ð\89ð\90Í
+///
+static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,JSONText **endptr);
+
+
+///
+///utf8\82Å\83I\83u\83W\83F\83N\83g\83\81\83\93\83o\81[\82ð\89ð\90Í
+///
+static JSONResult ParseObjectMemberUTF8(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);
+
+
+///
+///utf8\82Å\95\8e\9a\97ñ\92l\82ð\89ð\90Í
+///
+static JSONResult ParseStringValueUTF8(JSONParser_P parser,JSONText *start,const JSONText **endptr);
+
+///
+///json\95\8e\9a\97ñ\82Ì\8fI\92[\82ð\8eæ\93¾
+///
+static JSONText* GetJSONStringEnd(const JSONText *start);
+
+
+///
+///json\95\8e\9a\97ñ\97Ì\88æ\8am\95Û
+///
+static JSONChar_P JSONString_alloc(JSONStringAllocator_P allocator,JSONUInt len);
+
+
+///
+///json\95\8e\9a\97ñ\97Ì\88æ\89ð\95ú
+///
+static JSONVoid JSONString_free(JSONStringAllocator_P allocator);
+
+///
+///utf8\95\8e\9a\97ñ\82©\82çutf16\95\8e\9a\97ñ\95Ï\8a·\82É\95K\97v\82È\92·\82³\82ð\8eæ\93¾
+///
+static JSONUInt getLenU8ToU16(const JSONText *start,const JSONText *end);
+
+///
+///utf8\95\8e\9a\97ñ\82©\82çutf16\95\8e\9a\97ñ\82É\95Ï\8a·
+///
+static JSONUInt U8ToU16(JSONChar_P buf,JSONUInt len,const JSONText *start,JSONText *end);
+
+
+
+
+
+JSONResult JSONParser_Parse(JSONParser_P parser,const JSONVoid *jsontext,JSONParameter encode){
+
+       JSONResult rslt;
+
+       //\83G\83\93\83R\81[\83h\83^\83C\83v\82Ì\94»\92è
+       switch(encode){
+
+               //utf8\82Ì\8fê\8d\87
+       case JSON_ENCODE_UTF8:
+
+               //utf8\97p\82Ì\89ð\90Í
+               JSONParser_ParseUTF8(parser,(const JSONText*)jsontext);
+
+               break;
+
+               //\92è\8b`\82³\82ê\82Ä\82È\82¢\83^\83C\83v\82Ì\8fê\8d\87
+       default:
+
+               rslt = JSON_ERR_ENCODE_UNKNOWN;
+
+               break;
+       }
+
+       return rslt;
+
+}
+
+
+
+static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsontext){
+
+       JSONResult rslt = JSON_ERR_OK;
+
+       //\83I\83u\83W\83F\83N\83g\96\94\82Í\94z\97ñ\82Ì\8aJ\8en\95\8e\9a\82ð\8c\9f\8dõ
+       jsontext = strpbrk(jsontext,"{[");
+
+       //NULL\82Ì\8fê\8d\87\8fI\97¹\82³\82¹\82é
+       if(jsontext == NULL){
+               
+               return rslt;
+
+       }
+
+       //\8d\\91¢\91Ì\83^\83C\83v\94»\92è
+       switch(jsontext[0]){
+
+               //\83I\83u\83W\83F\83N\83g
+       case '{':
+
+               parser->objectHandler(JSONTypeRootObject,NULL,(JSONParameter)NULL);
+
+               break;
+
+               //\94z\97ñ
+       case '[':
+
+               parser->objectHandler(JSONTypeRootArray,NULL,(JSONParameter)NULL);
+
+
+               break;
+
+       }
+
+       return rslt;
+
+}
+
+
+static JSONResult ParseObjectUTF8(JSONParser_P parser,const JSONText *jsontext,JSONText **endptr){
+
+       JSONResult rslt;
+
+       
+
+       JSONChar_P name;
+
+       
+
+
+       //'{'\82ð\83X\83L\83b\83v
+       jsontext++;
+
+       
+
+       //\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");
+
+
+               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 '"':
+
+
+                       break;
+
+                       //\82»\82ê\88È\8aO\82Ì\95\8e\9a\82Ì\8fê\8d\87\82Í\8d\\95\83G\83\89\81[
+               default:
+
+                       goto syntaxerr;
+               
+               
+               }
+
+
+
+       }while(jsontext[0] == ',');
+
+end:
+       (*endptr) = (JSONText*)jsontext;
+
+       return rslt;
+
+       //\8d\\95\89ð\90Í\83G\83\89\81[
+syntaxerr:
+       rslt = JSON_ERR_SYNTAX;
+       goto end;
+
+
+
+}
+
+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;
+       JSONChar_P name;
+       JSONText *nameend;                                                                      //name\8fI\92[\95\8e\9a\83|\83C\83\93\83^
+       JSONUInt nameLen;                                                                       //name\82Ì\92·\82³
+
+       //'"'\82ð\83X\83L\83b\83v
+       jsontext++;
+
+       //\8fI\92[\95\8e\9a\83|\83C\83\93\83^\82ð\8eæ\93¾
+       nameend = GetJSONStringEnd(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);
+
+       //\95\8e\9a\97ñ\83o\83b\83t\83@\8eæ\93¾
+       name = JSONString_alloc(&stringAllocator,nameLen + 1);
+
+       //\83I\83u\83W\83F\83N\83g\96¼\82ð\89ð\90Í
+       rslt = ParseString(name,jsontext,&jsontext);
+
+       //\90³\8fí\82Å\82Í\82È\82¢\8fê\8d\87\8fI\97¹
+       if(rslt != JSON_ERR_OK){
+
+               goto end;
+
+       }
+
+       //\83\81\83\93\83o\83Z\83p\83\8c\81[\83^\82Ì\94»\92è
+       if(jsontext[0] != ':'){
+
+               goto syntaxerr;
+
+       }
+       
+       //\8bó\94\92\82ð\83X\83L\83b\83v
+       jsontext += strspn(jsontext," \r\n\t");
+
+
+       //value\82ð\89ð\90Í
+       switch(jsontext[0]){
+
+               //value\82ª\83I\83u\83W\83F\83N\83g\82Ì\8fê\8d\87
+       case '{':
+
+               break;
+
+               //value\82ª\94z\97ñ\82Ì\8fê\8d\87
+       case '[':
+
+               break;
+
+               //value\82ª\95\8e\9a\97ñ\82Ì\8fê\8d\87
+       case '"':
+
+               break;
+
+               //value\82ª\90\94\92l\82Ì\8fê\8d\87
+       case '-':
+       case '0':
+       case '1':
+       case '2':
+       case '3':
+       case '4':
+       case '5':
+       case '6':
+       case '7':
+       case '8':
+       case '9':
+
+               break;
+
+
+
+
+       }
+
+
+
+end:
+
+       //\95\8e\9a\97ñ\83o\83b\83t\83@\8aJ\95ú
+       JSONString_free(&stringAllocator);
+       (*endptr) = (JSONText*)jsontext;
+
+       return rslt;
+
+       //\8d\\95\89ð\90Í\83G\83\89\81[
+syntaxerr:
+       rslt = JSON_ERR_SYNTAX;
+       goto end;
+}
+
+
+
+
+JSONParser_P JSONParser_new(){
+
+       JSONParser_P parser = (JSONParser_P)malloc(sizeof(JSONParser));
+
+       parser->strValueSize = sizeof(JSONChar) * 1024;
+
+       parser->strValue = (JSONChar_P)malloc(parser->strValueSize);
+       parser->objectHandler = defaultObjectEvent;
+       parser->arrayHandler = defaultArrayEvent;
+       parser->objectEndHandler = defaultObjectEndEvent;
+       parser->arrayEndHandler = defaultArrayEndEvent;
+
+       return parser;
+}
+
+
+JSONVoid JSONParser_delete(JSONParser_P *parser){
+
+       
+       free((*parser)->strValue);
+       free(*parser);
+
+       parser = NULL;
+
+
+       return;
+}
+
+
+///
+///\83I\83u\83W\83F\83N\83g\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+///
+JSONVoid JSONParser_setObjectDataEventHandler(JSONParser_P parser,JSONObjectDataEventHandler handler){
+
+       parser->objectHandler = handler;
+
+       return;
+}
+
+
+///
+///\94z\97ñ\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+///
+JSONVoid JSONParser_setArrayDataEventHandler(JSONParser_P parser,JSONArrayDataEventHandler handler){
+
+       parser->arrayHandler = handler;
+
+       return;
+
+
+}
+
+///
+///\83I\83u\83W\83F\83N\83g\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+///
+JSONVoid JSONParser_setObjectEndEventHandler(JSONParser_P parser,JSONObjectEndHandler handler){
+
+       parser->objectEndHandler = handler;
+
+       return;
+
+}
+
+
+///
+///\94z\97ñ\8fI\92[\83C\83x\83\93\83g\83n\83\93\83h\83\89\82ð\83Z\83b\83g
+///
+JSONVoid JSONParser_setArrayEndEventHandler(JSONParser_P parser,JSONArrayEndHandler handler){
+
+       parser->arrayEndHandler = handler;
+
+       return;
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+//
+//\83v\83\89\83C\83x\81[\83g\8aÖ\90\94
+//
+
+static JSONVoid defaultObjectEvent(JSONValueType valueType,JSONChar_P name,JSONParameter parameter){
+
+
+
+
+}
+
+static JSONVoid defaultArrayEvent(JSONValueType valueType,JSONParameter parameter){
+
+
+
+}
+
+static JSONVoid defaultObjectEndEvent(JSONChar_P name){
+
+
+}
+
+static JSONVoid defaultArrayEndEvent(JSONChar_P name){
+
+
+
+}
\ No newline at end of file