|
|
| version 1.14, 2011/05/18 01:25:06 | version 1.16, 2011/11/30 06:15:26 |
|---|---|
| Line 39 struct Json { | Line 39 struct Json { |
| String* key; | String* key; |
| Value* result; | Value* result; |
| Junction* hook; | Junction* hook_object; |
| Junction* hook_array; | |
| Request* request; | Request* request; |
| Charset *charset; | Charset *charset; |
| bool handle_double; | bool handle_double; |
| enum Distinct { D_EXCEPTION, D_FIRST, D_LAST, D_ALL } distinct; | enum Distinct { D_EXCEPTION, D_FIRST, D_LAST, D_ALL } distinct; |
| Json(Charset* acharset): stack(), key_stack(), key(NULL), result(NULL), hook(NULL), request(NULL), charset(acharset), handle_double(true), distinct(D_EXCEPTION){} | Json(Charset* acharset): stack(), key_stack(), key(NULL), result(NULL), hook_object(NULL), hook_array(NULL), request(NULL), charset(acharset), handle_double(true), distinct(D_EXCEPTION){} |
| bool set_distinct(const String &value){ | bool set_distinct(const String &value){ |
| if (value == "first") distinct = D_FIRST; | if (value == "first") distinct = D_FIRST; |
| Line 109 static int json_callback(Json *json, int | Line 110 static int json_callback(Json *json, int |
| switch(type) { | switch(type) { |
| case JSON_T_OBJECT_BEGIN:{ | case JSON_T_OBJECT_BEGIN:{ |
| VHash *v = new VHash(); | VHash *v = new VHash(); |
| if (json->hook){ | if (json->hook_object){ |
| json->key_stack.push(json->key); | json->key_stack.push(json->key); |
| json->key=NULL; | |
| } else { | } else { |
| if (json->stack.count()) set_json_value(json, v); | if (json->stack.count()) set_json_value(json, v); |
| } | } |
| Line 118 static int json_callback(Json *json, int | Line 120 static int json_callback(Json *json, int |
| break; | break; |
| } | } |
| case JSON_T_OBJECT_END:{ | case JSON_T_OBJECT_END:{ |
| if (json->hook){ | if (json->hook_object){ |
| String* key = json->key_stack.pop(); | String* key = json->key_stack.pop(); |
| json->result = json_hook(*json->request, json->hook, key, json->stack.pop()); | json->result = json_hook(*json->request, json->hook_object, key, json->stack.pop()); |
| if (json->stack.count()){ | if (json->stack.count()){ |
| json->key = key; | json->key = key; |
| Line 133 static int json_callback(Json *json, int | Line 135 static int json_callback(Json *json, int |
| } | } |
| case JSON_T_ARRAY_BEGIN:{ | case JSON_T_ARRAY_BEGIN:{ |
| VHash *v = new VHash(); | VHash *v = new VHash(); |
| if (json->stack.count()) set_json_value(json, v); | if (json->hook_array){ |
| json->key_stack.push(json->key); | |
| json->key=NULL; | |
| } else { | |
| if (json->stack.count()) set_json_value(json, v); | |
| } | |
| json->stack.push(v); | json->stack.push(v); |
| break; | break; |
| } | } |
| case JSON_T_ARRAY_END: | case JSON_T_ARRAY_END: |
| // libjson supports array at top level, we too | // libjson supports array at top level, we too |
| json->result = json->stack.pop(); | if (json->hook_array){ |
| String* key = json->key_stack.pop(); | |
| json->result = json_hook(*json->request, json->hook_array, key, json->stack.pop()); | |
| if (json->stack.count()){ | |
| json->key = key; | |
| set_json_value(json, json->result); | |
| } | |
| } else { | |
| json->result = json->stack.pop(); | |
| } | |
| break; | break; |
| case JSON_T_KEY: | case JSON_T_KEY: |
| json->key = json_string(json, value); | json->key = json_string(json, value); |
| break; | break; |
| case JSON_T_INTEGER: | case JSON_T_INTEGER: |
| set_json_value(json, new VInt((int)value->vu.integer_value)); | set_json_value(json, new VInt((int)value->vu.integer_value)); |
| break; | break; |
| Line 217 static void _parse(Request& r, MethodPar | Line 234 static void _parse(Request& r, MethodPar |
| valid_options++; | valid_options++; |
| } | } |
| if(Value* value=options->get("object")) { | if(Value* value=options->get("object")) { |
| json.hook=value->get_junction(); | json.hook_object=value->get_junction(); |
| json.request=&r; | json.request=&r; |
| if (!json.hook || !json.hook->method || !json.hook->method->params_names || !(json.hook->method->params_names->count() == 2)) | if (!json.hook_object || !json.hook_object->method || !json.hook_object->method->params_names || !(json.hook_object->method->params_names->count() == 2)) |
| throw Exception(PARSER_RUNTIME, 0, "$.object must be parser method with 2 parameters"); | throw Exception(PARSER_RUNTIME, 0, "$.object must be parser method with 2 parameters"); |
| valid_options++; | valid_options++; |
| } | } |
| if(Value* value=options->get("array")) { | |
| json.hook_array=value->get_junction(); | |
| json.request=&r; | |
| if (!json.hook_array || !json.hook_array->method || !json.hook_array->method->params_names || !(json.hook_array->method->params_names->count() == 2)) | |
| throw Exception(PARSER_RUNTIME, 0, "$.array must be parser method with 2 parameters"); | |
| valid_options++; | |
| } | |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| Line 299 const String& hash_json_string(HashStrin | Line 323 const String& hash_json_string(HashStrin |
| return result; | return result; |
| } | } |
| static bool based_on( | |
| HashStringValue::key_type key, | |
| HashStringValue::value_type /*value*/, | |
| Value* v) { | |
| return v->is(key.cstr()); | |
| } | |
| const String& value_json_string(String::Body key, Value& v, Json_options* options) { | const String& value_json_string(String::Body key, Value& v, Json_options* options) { |
| if(options && options->methods) | if(options && options->methods) { |
| if(Value* method=options->methods->get(v.type())){ | Value* method=options->methods->get(v.type()); |
| if(!method) | |
| method=options->methods->first_that<Value*>(based_on, &v); | |
| if(method) { | |
| Junction* junction=method->get_junction(); | Junction* junction=method->get_junction(); |
| VMethodFrame frame(*junction->method, options->r->method_frame, junction->self); | VMethodFrame frame(*junction->method, options->r->method_frame, junction->self); |
| Line 312 const String& value_json_string(String:: | Line 346 const String& value_json_string(String:: |
| return frame.result().as_string(); | return frame.result().as_string(); |
| } | } |
| } | |
| if(HashStringValue* hash=v.get_hash()) | if(HashStringValue* hash=v.get_hash()) |
| return hash_json_string(*hash, options); | return hash_json_string(*hash, options); |