Last active
December 19, 2015 00:00
-
-
Save kosh04/5865905 to your computer and use it in GitHub Desktop.
JSONパーサ <https://github.com/kgabis/parson> を #newlisp に組み込むとこんな感じになる。
エラー無視。パースのみ。シリアライズなし。追加引数を与えるとドット記法での探索が可能。
処理速度は json-parse の方が早い。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* nl-json.c */ | |
#include "newlisp.h" | |
#include "protos.h" | |
#include "nl-json.h" | |
CELL * stuffList(int length, ...) | |
{ | |
CELL * list; | |
int i; | |
va_list ap; | |
va_start(ap, length); | |
list = getCell(CELL_EXPRESSION); | |
for (i = 0; i < length; i++) { | |
addList(list, va_arg(ap, CELL *)); | |
} | |
va_end(ap); | |
return list; | |
} | |
CELL * makePair(CELL * left, CELL * right) | |
{ | |
return stuffList(2, left, right); | |
} | |
CELL * json_value_to_cell(JSON_Value * value) | |
{ | |
switch (json_value_get_type(value)) | |
{ | |
case JSONNull: | |
return copyCell(nilCell); | |
case JSONString: | |
return stuffString((char *)json_value_get_string(value)); | |
case JSONNumber: { | |
double num = json_value_get_number(value); | |
return stuffFloat(&num); | |
} | |
case JSONObject: { | |
JSON_Object * obj = json_value_get_object(value); | |
return json_object_to_cell(obj); | |
} | |
case JSONArray: { | |
JSON_Array * arr = json_value_get_array(value); | |
return json_array_to_cell(arr); | |
} | |
case JSONBoolean: { | |
int bool = json_value_get_boolean(value); | |
SYMBOL * x = translateCreateSymbol((bool)?"true":"false", CELL_SYMBOL, mainContext, TRUE); | |
return stuffSymbol(x); | |
} | |
default: | |
return nilCell; | |
} | |
} | |
CELL * json_object_to_cell(JSON_Object * obj) | |
{ | |
CELL * assoc; | |
int i; | |
assoc = getCell(CELL_EXPRESSION); | |
for (i = 0; i < json_object_get_count(obj); i++) { | |
char * name = (char *)json_object_get_name(obj, i); | |
JSON_Value * value = json_object_get_value(obj, name); | |
addList(assoc, makePair(stuffString(name), json_value_to_cell(value))); | |
} | |
return assoc; | |
} | |
CELL * json_array_to_cell(JSON_Array * arr) | |
{ | |
CELL * cell; | |
int i; | |
cell = getCell(CELL_EXPRESSION); | |
for (i = 0; i < json_array_get_count(arr); i++) { | |
JSON_Value * value = json_array_get_value(arr, i); | |
addList(cell, json_value_to_cell(value)); | |
} | |
return cell; | |
} | |
/* (json-decode "JSON_TEXT") ;=> LIST/nil */ | |
/* (json-decode "JSON_TEXT" "obj_a.obj_b.value") ;=> LIST/nil */ | |
CELL * p_json_decode(CELL * params) | |
{ | |
char * data; | |
char * name; | |
CELL * cell = nilCell; | |
JSON_Value * root; | |
JSON_Value_Type type; | |
params = getString(params, &data); | |
root = json_parse_string(data); | |
type = json_value_get_type(root); | |
if (params != nilCell) { | |
JSON_Value * value; | |
getString(params, &name); | |
value = json_object_dotget_value(json_value_get_object(root), name); | |
cell = json_value_to_cell(value); | |
} else if (type == JSONObject || type == JSONArray) { | |
cell = json_value_to_cell(root); | |
} | |
json_value_free(root); | |
return cell; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* nl-json.h */ | |
#ifndef _nl_json_h | |
#define _nl_json_h | |
#include "newlisp.h" | |
#include "kgabis-parson/parson.h" | |
CELL * p_json_decode(CELL *); | |
CELL * json_value_to_cell(JSON_Value *); | |
CELL * json_object_to_cell(JSON_Object *); | |
CELL * json_array_to_cell(JSON_Array *); | |
#endif /* _nl_json_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- primes.h.orig 2013-06-04 11:44:59 +0900 | |
+++ primes.h 2013-06-26 15:58:55 +0900 | |
@@ -20,6 +20,7 @@ | |
#ifndef PRIMES_H | |
#define PRIMES_H | |
+#include "nl-json.h" | |
PRIMITIVE primitive[] = | |
{ | |
@@ -450,6 +455,7 @@ | |
{"net-ping", p_netPing, 0}, | |
#endif | |
{"net-error", p_netLastError, 0}, | |
+ {"json-decode", p_json_decode, 0}, | |
{NULL,NULL,0}, | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment