Annotation of parser3/src/classes/json.C, revision 1.26

1.1       misha       1: /** @file
                      2:        Parser: @b json parser class.
                      3: 
1.17      moko        4:        Copyright (c) 2000-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       misha       5: */
                      6: 
                      7: #include "classes.h"
                      8: #include "pa_vmethod_frame.h"
                      9: 
                     10: #include "pa_request.h"
                     11: #include "pa_vbool.h"
                     12: 
                     13: #include "pa_charset.h"
                     14: #include "pa_charsets.h"
1.25      moko       15: #include "json.h"
1.1       misha      16: 
1.14      misha      17: #ifdef XML
                     18: #include "pa_vxdoc.h"
                     19: #endif
                     20: 
1.26    ! moko       21: volatile const char * IDENT_JSON_C="$Id: json.C,v 1.25 2013/07/29 09:44:16 moko Exp $";
1.17      moko       22: 
1.1       misha      23: // class
                     24: 
                     25: class MJson: public Methoded {
                     26: public:
                     27:        MJson();
                     28: };
                     29: 
                     30: // global variable
                     31: 
                     32: DECLARE_CLASS_VAR(json, new MJson, 0);
                     33: 
                     34: // methods
                     35: struct Json {
1.4       moko       36:        Stack<VHash*> stack;
1.3       moko       37:        Stack<String*> key_stack;
1.1       misha      38: 
1.3       moko       39:        String* key;
1.1       misha      40:        Value* result;
                     41: 
1.16      misha      42:        Junction* hook_object;
                     43:        Junction* hook_array;
1.3       moko       44:        Request* request;
                     45: 
1.1       misha      46:        Charset *charset;
1.23      moko       47:        String::Language taint;
                     48: 
1.1       misha      49:        bool handle_double;
1.4       moko       50:        enum Distinct { D_EXCEPTION, D_FIRST, D_LAST, D_ALL } distinct;
1.3       moko       51: 
1.23      moko       52:        Json(Charset* acharset): stack(), key_stack(), key(NULL), result(NULL), hook_object(NULL), hook_array(NULL), 
                     53:                request(NULL), charset(acharset), taint(String::L_TAINTED), handle_double(true), distinct(D_EXCEPTION){}
1.4       moko       54: 
                     55:        bool set_distinct(const String &value){
                     56:                if (value == "first") distinct = D_FIRST;
                     57:                else if (value == "last") distinct = D_LAST;
                     58:                else if (value == "all") distinct = D_ALL;
                     59:                else return false;
                     60:                return true;
                     61:        }
1.1       misha      62: };
                     63: 
                     64: static void set_json_value(Json *json, Value *value){
1.4       moko       65:        VHash *top = json->stack.top_value();
1.3       moko       66:        if(json->key == NULL){
1.4       moko       67:                top->hash().put(String(format(top->get_hash()->count(), 0)), value);
1.1       misha      68:        } else {
1.4       moko       69:                switch (json->distinct){
                     70:                        case Json::D_EXCEPTION:
                     71:                                if (top->hash().put_dont_replace(*json->key, value))
                     72:                                        throw Exception(PARSER_RUNTIME, json->key, "duplicate key");
                     73:                                break;
                     74:                        case Json::D_FIRST:
                     75:                                top->hash().put_dont_replace(*json->key, value);
                     76:                                break;
                     77:                        case Json::D_LAST:
                     78:                                top->hash().put(*json->key, value);
                     79:                                break;
                     80:                        case Json::D_ALL:
                     81:                                if (top->hash().put_dont_replace(*json->key, value)){
                     82:                                        for(int i=2;;i++){
                     83:                                                String key;
                     84:                                                key << *json->key << "_" << format(i, 0);
                     85:                                                if (!top->hash().put_dont_replace(key, value)) break;
                     86:                                        }
                     87:                                }
                     88:                                break;
                     89:                }
1.3       moko       90:                json->key=NULL;
1.1       misha      91:        }
                     92: }
                     93: 
1.25      moko       94: String* json_string(Json *json, const char *value, uint32_t length){
1.3       moko       95:        String::C result = json->charset !=NULL ? 
1.25      moko       96:                Charset::transcode(String::C(value, length), UTF8_charset, *json->charset) :
                     97:                String::C(pa_strdup(value, length), length);
1.23      moko       98:        return new String(result.str, json->taint, result.length);
1.1       misha      99: }
                    100: 
1.3       moko      101: static Value *json_hook(Request &r, Junction *hook, String* key, Value* value){
                    102:        VMethodFrame frame(*hook->method, r.method_frame, hook->self);
1.10      moko      103:        Value *params[]={new VString(key ? *key : String::Empty), value};
1.3       moko      104: 
                    105:        frame.store_params(params, 2);
                    106:        r.execute_method(frame);
                    107: 
                    108:        return &frame.result().as_value();
1.1       misha     109: }
                    110: 
1.25      moko      111: static int json_callback(Json *json, int type, const char *value, uint32_t length)
1.1       misha     112: {
                    113:        switch(type) {
1.25      moko      114:                case JSON_OBJECT_BEGIN:{
1.4       moko      115:                        VHash *v = new VHash();
1.16      misha     116:                        if (json->hook_object){
1.1       misha     117:                                json->key_stack.push(json->key);
1.16      misha     118:                                json->key=NULL;
1.1       misha     119:                        } else {
                    120:                                if (json->stack.count()) set_json_value(json, v);
                    121:                        }
                    122:                        json->stack.push(v);
                    123:                        break;
                    124:                }
1.25      moko      125:                case JSON_OBJECT_END:{
1.16      misha     126:                        if (json->hook_object){
1.3       moko      127:                                String* key = json->key_stack.pop();
1.16      misha     128:                                json->result = json_hook(*json->request, json->hook_object, key, json->stack.pop());
1.1       misha     129:                                
                    130:                                if (json->stack.count()){
                    131:                                        json->key = key;
                    132:                                        set_json_value(json, json->result);
                    133:                                }
                    134:                        } else {
                    135:                                json->result = json->stack.pop();
                    136:                        }
                    137:                        break;
                    138:                }
1.25      moko      139:                case JSON_ARRAY_BEGIN:{
1.4       moko      140:                        VHash *v = new VHash();
1.16      misha     141:                        if (json->hook_array){
                    142:                                json->key_stack.push(json->key);
                    143:                                json->key=NULL;
                    144:                        } else {
                    145:                                if (json->stack.count()) set_json_value(json, v);
                    146:                        }
1.1       misha     147:                        json->stack.push(v);
                    148:                        break;
                    149:                }
1.25      moko      150:                case JSON_ARRAY_END:
1.12      moko      151:                        // libjson supports array at top level, we too
1.16      misha     152:                        if (json->hook_array){
                    153:                                String* key = json->key_stack.pop();
                    154:                                json->result = json_hook(*json->request, json->hook_array, key, json->stack.pop());
                    155:                                
                    156:                                if (json->stack.count()){
                    157:                                        json->key = key;
                    158:                                        set_json_value(json, json->result);
                    159:                                }
                    160:                        } else {
                    161:                                json->result = json->stack.pop();
                    162:                        }
1.1       misha     163:                        break;
1.25      moko      164:                case JSON_KEY:
                    165:                        json->key = json_string(json, value, length);
1.16      misha     166:                        break;
1.25      moko      167:                case JSON_INT:
                    168:                        set_json_value(json, new VDouble( json_string(json, value, length)->as_double() ));
1.1       misha     169:                        break;
1.25      moko      170:                case JSON_FLOAT:
1.1       misha     171:                        if (json->handle_double){
1.25      moko      172:                                set_json_value(json, new VDouble( json_string(json, value, length)->as_double() ));
1.1       misha     173:                                break;
1.25      moko      174:                        } // else is JSON_STRING
                    175:                case JSON_STRING:
                    176:                        set_json_value(json, new VString(*json_string(json, value, length)));
1.1       misha     177:                        break;
1.25      moko      178:                case JSON_NULL:
1.18      moko      179:                        set_json_value(json, VVoid::get());
1.1       misha     180:                        break;
1.25      moko      181:                case JSON_TRUE:
1.1       misha     182:                        set_json_value(json, &VBool::get(true));
                    183:                        break;
1.25      moko      184:                case JSON_FALSE:
1.1       misha     185:                        set_json_value(json, &VBool::get(false));
1.25      moko      186:                        break;
1.1       misha     187:        }
1.25      moko      188:        return 0;
1.1       misha     189: }
                    190: 
1.5       moko      191: static const char* json_error_message(int error_code){
                    192:        static const char* error_messages[] = {
1.1       misha     193:                NULL,
1.25      moko      194:                "out of memory",
                    195:                "bad character",
                    196:                "stack empty",
                    197:                "pop unexpected mode",
                    198:                "nesting limit",
                    199:                "data limit",
                    200:                "comment not allowed by config",
                    201:                "unexpected char",
                    202:                "missing unicode low surrogate",
                    203:                "unexpected unicode low surrogate",
                    204:                "error comma out of structure",
                    205:                "error in a callback"
1.1       misha     206:        };
                    207:        return error_messages[error_code];
                    208: }
                    209: 
1.23      moko      210: extern String::Language get_untaint_lang(const String& lang_name);
                    211: 
1.1       misha     212: static void _parse(Request& r, MethodParams& params) {
1.3       moko      213:        const String& json_string=params.as_string(0, "json must be string");
                    214: 
                    215:        Json json(r.charsets.source().isUTF8() ? NULL : &(r.charsets.source()));
1.1       misha     216: 
1.25      moko      217:        json_config config = {
                    218:                0,              // buffer_initial_size
1.26    ! moko      219:                128,            // max_nesting
1.25      moko      220:                0,              // max_data
                    221:                1,              // allow_c_comments
                    222:                1,              // allow_yaml_comments
                    223:                pa_malloc,
                    224:                pa_realloc,
                    225:                pa_free
                    226:        };
1.1       misha     227: 
                    228:        if(params.count() == 2)
                    229:                if(HashStringValue* options=params.as_hash(1)) {
                    230:                        int valid_options=0;
                    231:                        if(Value* value=options->get("depth")) {
1.25      moko      232:                                config.max_nesting=r.process_to_value(*value).as_int();
1.1       misha     233:                                valid_options++;
                    234:                        }
                    235:                        if(Value* value=options->get("double")) {
1.4       moko      236:                                json.handle_double=r.process_to_value(*value).as_bool();
                    237:                                valid_options++;
                    238:                        }
                    239:                        if(Value* value=options->get("distinct")) {
                    240:                                const String& sdistinct=value->as_string();
                    241:                                if (!json.set_distinct(sdistinct))
                    242:                                        throw Exception(PARSER_RUNTIME, &sdistinct, "must be 'first', 'last' or 'all'");
1.1       misha     243:                                valid_options++;
                    244:                        }
1.23      moko      245:                        if(Value* value=options->get("taint")) {
                    246:                                json.taint=get_untaint_lang(value->as_string());
                    247:                                valid_options++;
                    248:                        }
1.1       misha     249:                        if(Value* value=options->get("object")) {
1.16      misha     250:                                json.hook_object=value->get_junction();
1.3       moko      251:                                json.request=&r;
1.16      misha     252:                                if (!json.hook_object || !json.hook_object->method || !json.hook_object->method->params_names || !(json.hook_object->method->params_names->count() == 2))
1.1       misha     253:                                        throw Exception(PARSER_RUNTIME, 0, "$.object must be parser method with 2 parameters");
                    254:                                valid_options++;
                    255:                        }
1.16      misha     256:                        if(Value* value=options->get("array")) {
                    257:                                json.hook_array=value->get_junction();
                    258:                                json.request=&r;
                    259:                                if (!json.hook_array || !json.hook_array->method || !json.hook_array->method->params_names || !(json.hook_array->method->params_names->count() == 2))
                    260:                                        throw Exception(PARSER_RUNTIME, 0, "$.array must be parser method with 2 parameters");
                    261:                                valid_options++;
                    262:                        }
1.1       misha     263:                        if(valid_options!=options->count())
                    264:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    265:                }
                    266: 
1.2       misha     267:        const String::Body json_body = json_string.cstr_to_string_body_untaint(String::L_JSON, 0, &(r.charsets));
1.1       misha     268:        const char *json_cstr = json.charset != NULL ? Charset::transcode(json_body, *json.charset, UTF8_charset).cstr() : json_body.cstr();
                    269: 
1.25      moko      270:        json_parser parser;
                    271:        if(int result = json_parser_init(&parser, &config, (json_parser_callback)&json_callback, &json))
                    272:                throw Exception("json.parse", 0, "%s", json_error_message(result));
                    273: 
                    274:        uint32_t processed;
                    275:        if(int result = json_parser_string(&parser, json_cstr, strlen(json_cstr), &processed))
                    276:                throw Exception("json.parse", 0, "%s at byte %d", json_error_message(result), processed);
1.3       moko      277: 
1.25      moko      278:        if (!json_parser_is_done(&parser))
                    279:                throw Exception("json.parse", 0, "unexpected end of json data");
1.1       misha     280:        
1.25      moko      281:        json_parser_free(&parser);
1.1       misha     282: 
                    283:        if (json.result) r.write_no_lang(*json.result);
                    284: }
                    285: 
1.26    ! moko      286: const uint ANTI_ENDLESS_JSON_STRING_RECOURSION=128;
        !           287: 
1.8       moko      288: char *get_indent(uint level){
                    289:        static char* cache[ANTI_ENDLESS_JSON_STRING_RECOURSION]={};
                    290:        if (!cache[level]){
                    291:                char *result =  static_cast<char*>(pa_gc_malloc_atomic(level+1));
                    292:                memset(result, '\t', level);
1.9       moko      293:                result[level]='\0';
1.8       moko      294:                return cache[level]=result;
                    295:        }
                    296:        return cache[level];
                    297: }
                    298: 
1.26    ! moko      299: class Json_string_recoursion {
        !           300:        Json_options& foptions;
        !           301: public:
        !           302:        Json_string_recoursion(Json_options& aoptions) : foptions(aoptions) {
        !           303:                if(++foptions.json_string_recoursion==ANTI_ENDLESS_JSON_STRING_RECOURSION)
        !           304:                        throw Exception(PARSER_RUNTIME, 0, "call canceled - endless json recursion detected");
        !           305:        }
        !           306:        ~Json_string_recoursion() {
        !           307:                if(foptions.json_string_recoursion)
        !           308:                        foptions.json_string_recoursion--;
        !           309:        }
        !           310: };
        !           311: 
1.21      moko      312: const String& value_json_string(String::Body key, Value& v, Json_options& options);
1.6       misha     313: 
1.21      moko      314: const String* Json_options::hash_json_string(HashStringValue &hash) {
1.6       misha     315:        if(!hash.count())
1.21      moko      316:                return new String("{}", String::L_AS_IS);
1.8       moko      317: 
1.26    ! moko      318:        Json_string_recoursion go_down(*this);
1.8       moko      319: 
                    320:        String& result = *new String("{\n", String::L_AS_IS);
                    321: 
1.21      moko      322:        if (indent){
1.8       moko      323: 
                    324:                String *delim=NULL;
1.26    ! moko      325:                indent=get_indent(json_string_recoursion);
1.8       moko      326:                for(HashStringValue::Iterator i(hash); i; i.next() ){
                    327:                        if (delim){
                    328:                                result << *delim;
                    329:                        } else {
1.21      moko      330:                                result << indent << "\"";
                    331:                                delim = new String(",\n", String::L_AS_IS); *delim << indent << "\"";
1.8       moko      332:                        }
1.21      moko      333:                        result << String(i.key(), String::L_JSON) << "\":" << value_json_string(i.key(), *i.value(), *this);
1.8       moko      334:                }
1.26    ! moko      335:                result << "\n" << (indent=get_indent(json_string_recoursion-1)) << "}";
1.6       misha     336: 
1.8       moko      337:        } else {
                    338: 
                    339:                bool need_delim=false;
                    340:                for(HashStringValue::Iterator i(hash); i; i.next() ){
                    341:                        result << (need_delim ? ",\n\"" : "\"");
1.21      moko      342:                        result << String(i.key(), String::L_JSON) << "\":" << value_json_string(i.key(), *i.value(), *this);
1.8       moko      343:                        need_delim=true;
                    344:                }
                    345:                result << "\n}";
1.6       misha     346: 
                    347:        }
                    348: 
1.21      moko      349:        return &result;
1.6       misha     350: }
                    351: 
1.21      moko      352: static bool based_on(HashStringValue::key_type key, HashStringValue::value_type /*value*/, Value* v) {
1.15      misha     353:        return v->is(key.cstr());
                    354: }
1.26    ! moko      355: 
1.21      moko      356: const String& value_json_string(String::Body key, Value& v, Json_options& options) {
                    357:        if(options.methods) {
                    358:                Value* method=options.methods->get(v.type());
                    359:                if(!method){
                    360:                        method=options.methods->first_that<Value*>(based_on, &v);
                    361:                        options.methods->put(key, method ? method : VVoid::get());
                    362:                }
                    363:                if(method && !method->is_void()) {
1.6       misha     364:                        Junction* junction=method->get_junction();
1.21      moko      365:                        VMethodFrame frame(*junction->method, options.r->method_frame, junction->self);
1.6       misha     366: 
1.26    ! moko      367:                        HashStringValue* params_hash=options.params && options.indent ? options.params->get_hash() : NULL;
        !           368:                        Temp_hash_value<HashStringValue, Value*> indent(params_hash, "indent", new VString(*new String(options.indent)));
        !           369: 
1.21      moko      370:                        Value *params[]={new VString(*new String(key, String::L_JSON)), &v, options.params ? options.params : VVoid::get()};
1.13      moko      371:                        frame.store_params(params, 3);
1.6       misha     372: 
1.21      moko      373:                        options.r->execute_method(frame);
1.6       misha     374: 
                    375:                        return frame.result().as_string();
                    376:                }
1.15      misha     377:        }
1.6       misha     378: 
1.21      moko      379:        options.key=key;
1.6       misha     380:        return *v.get_json_string(options);
                    381: }
                    382: 
                    383: static void _string(Request& r, MethodParams& params) {
                    384:        Json_options json(&r);
                    385: 
                    386:        if(params.count() == 2)
                    387:                if(HashStringValue* options=params.as_hash(1)) {
                    388:                        json.params=params.get(1);
                    389:                        HashStringValue* methods=new HashStringValue();
                    390:                        int valid_options=0;
1.14      misha     391:                        HashStringValue* vvalue;
1.6       misha     392:                        for(HashStringValue::Iterator i(*options); i; i.next() ){
                    393:                                String::Body key=i.key();
                    394:                                Value* value=i.value();
                    395:                                if(key == "skip-unknown"){
                    396:                                        json.skip_unknown=r.process_to_value(*value).as_bool();
                    397:                                        valid_options++;
                    398:                                } else if(key == "date" && value->is_string()){
                    399:                                        const String& svalue=value->as_string();
                    400:                                        if(!json.set_date_format(svalue))
                    401:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'sql-string', 'gmt-string' or 'unix-timestamp'");
                    402:                                        valid_options++;
1.8       moko      403:                                } else if(key == "indent"){
1.26    ! moko      404:                                        if(value->is_string()){
        !           405:                                                json.indent=value->as_string().cstr();
        !           406:                                                json.json_string_recoursion=strlen(json.indent);
        !           407:                                        } else json.indent=r.process_to_value(*value).as_bool() ? "" : NULL;
1.8       moko      408:                                        valid_options++;
1.6       misha     409:                                } else if(key == "table" && value->is_string()){
                    410:                                        const String& svalue=value->as_string();
                    411:                                        if(!json.set_table_format(svalue))
1.13      moko      412:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'array', 'object' or 'compact'");
1.6       misha     413:                                        valid_options++;
                    414:                                } else if(key == "file" && value->is_string()){
                    415:                                        const String& svalue=value->as_string();
                    416:                                        if(!json.set_file_format(svalue))
1.19      misha     417:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'base64', 'text' or 'stat'");
1.6       misha     418:                                        valid_options++;
1.14      misha     419: #ifdef XML
                    420:                                } else if(key == "xdoc" && (vvalue = value->get_hash())){
1.24      moko      421:                                        json.xdoc_options=new XDocOutputOptions();
                    422:                                        json.xdoc_options->append(r, vvalue);
1.14      misha     423:                                        valid_options++;
                    424: #endif
1.6       misha     425:                                } else if(Junction* junction=value->get_junction()){
1.13      moko      426:                                        if(!junction->method || !junction->method->params_names || junction->method->params_names->count() != 3)
                    427:                                                throw Exception(PARSER_RUNTIME, 0, "$.%s must be parser method with 3 parameters", key.cstr());
1.6       misha     428:                                        methods->put(key, value);
                    429:                                        valid_options++;
                    430:                                }
                    431:                        }
1.22      moko      432: 
1.6       misha     433:                        if(valid_options!=options->count())
                    434:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.22      moko      435: 
                    436:                        // special handling for $._default 
                    437:                        if(VHash* vhash=static_cast<VHash*>(params[1].as(VHASH_TYPE)))
                    438:                                if(Value* value=vhash->get_default()) {
                    439:                                        Junction* junction=value->get_junction();
                    440:                                        if(!junction || !junction->method || !junction->method->params_names || junction->method->params_names->count() != 3)
                    441:                                                throw Exception(PARSER_RUNTIME, 0, "$.%s must be parser method with 3 parameters", HASH_DEFAULT_ELEMENT_NAME);
                    442:                                        json.default_method=value;
                    443:                                }
                    444: 
1.6       misha     445:                        if(methods->count())
                    446:                                json.methods=methods;
                    447:                }
1.14      misha     448: 
1.21      moko      449:        const String& result_string=value_json_string(String::Body(), params[0], json);
1.13      moko      450:        String::Body result_body=result_string.cstr_to_string_body_untaint(String::L_JSON, 0, &r.charsets);
                    451:        r.write_pass_lang(*new String(result_body, String::L_AS_IS));
1.6       misha     452:  }
                    453: 
1.1       misha     454: // constructor
                    455: 
                    456: MJson::MJson(): Methoded("json") {
                    457:        add_native_method("parse", Method::CT_STATIC, _parse, 1, 2);
1.6       misha     458: 
                    459:        add_native_method("string", Method::CT_ANY, _string, 1, 2);
1.1       misha     460: }

E-mail: