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

1.1       misha       1: /** @file
                      2:        Parser: @b json parser class.
                      3: 
1.66    ! moko        4:        Copyright (c) 2000-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.57      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>
1.1       misha       6: */
                      7: 
                      8: #include "classes.h"
                      9: #include "pa_vmethod_frame.h"
                     10: 
                     11: #include "pa_request.h"
                     12: #include "pa_vbool.h"
1.60      moko       13: #include "pa_varray.h"
1.1       misha      14: 
                     15: #include "pa_charset.h"
                     16: #include "pa_charsets.h"
1.29      moko       17: #include "pa_json.h"
1.1       misha      18: 
1.14      misha      19: #ifdef XML
                     20: #include "pa_vxdoc.h"
                     21: #endif
                     22: 
1.66    ! moko       23: volatile const char * IDENT_JSON_C="$Id: json.C,v 1.65 2024/10/20 14:30:14 moko Exp $";
1.17      moko       24: 
1.1       misha      25: // class
                     26: 
                     27: class MJson: public Methoded {
                     28: public:
                     29:        MJson();
1.64      moko       30: 
                     31:        override Value* get_element(const String&);
                     32:        override const VJunction* put_element(const String&, Value*);
1.1       misha      33: };
                     34: 
                     35: // global variable
                     36: 
1.41      moko       37: DECLARE_CLASS_VAR(json, new MJson);
1.1       misha      38: 
1.64      moko       39: bool handle_array_default=true;
                     40: 
1.1       misha      41: // methods
1.53      moko       42: struct Json : public PA_Allocated {
1.59      moko       43:        Stack<VHashBase*> stack;
1.3       moko       44:        Stack<String*> key_stack;
1.1       misha      45: 
1.3       moko       46:        String* key;
1.1       misha      47:        Value* result;
                     48: 
1.16      misha      49:        Junction* hook_object;
                     50:        Junction* hook_array;
1.3       moko       51:        Request* request;
                     52: 
1.1       misha      53:        Charset *charset;
1.23      moko       54:        String::Language taint;
                     55: 
1.1       misha      56:        bool handle_double;
1.30      misha      57:        bool handle_int;
1.59      moko       58:        bool handle_array;
1.4       moko       59:        enum Distinct { D_EXCEPTION, D_FIRST, D_LAST, D_ALL } distinct;
1.3       moko       60: 
1.23      moko       61:        Json(Charset* acharset): stack(), key_stack(), key(NULL), result(NULL), hook_object(NULL), hook_array(NULL), 
1.30      misha      62:                request(NULL), charset(acharset), taint(String::L_TAINTED), handle_double(true), handle_int(true), 
1.64      moko       63:                handle_array(handle_array_default), distinct(D_EXCEPTION){}
1.4       moko       64: 
                     65:        bool set_distinct(const String &value){
                     66:                if (value == "first") distinct = D_FIRST;
                     67:                else if (value == "last") distinct = D_LAST;
                     68:                else if (value == "all") distinct = D_ALL;
                     69:                else return false;
                     70:                return true;
                     71:        }
1.59      moko       72: 
                     73:        bool set_handle_array(const String &value){
                     74:                if (value == "array") handle_array = true;
                     75:                else if (value == "hash") handle_array = false;
                     76:                else return false;
                     77:                return true;
                     78:        }
1.1       misha      79: };
                     80: 
1.64      moko       81: 
                     82: Value* MJson::get_element(const String& aname) {
                     83:        if (aname=="array"){
                     84:                return new VString(*new String(handle_array_default ? "array" : "hash"));
                     85:        }
                     86:        return Methoded::get_element(aname);
                     87: }
                     88: 
                     89: const VJunction* MJson::put_element(const String& aname, Value* avalue) {
                     90:        if (aname=="array"){
                     91:                Json json(NULL);
                     92:                if (avalue->get_string()){
                     93:                        const String& sarray=avalue->as_string();
                     94:                        if (json.set_handle_array(sarray)){
                     95:                                handle_array_default=json.handle_array;
                     96:                                return 0;
                     97:                        }
                     98:                        throw Exception(PARSER_RUNTIME, &sarray, "$json:array must be 'array' or 'hash'");
                     99:                }
                    100:                throw Exception(PARSER_RUNTIME, 0, "$json:array must be 'array' or 'hash'");
                    101:        }
                    102:        return Methoded::put_element(aname, avalue);
                    103: }
                    104: 
                    105: 
1.1       misha     106: static void set_json_value(Json *json, Value *value){
1.59      moko      107:        VHashBase *top = json->stack.top_value();
1.3       moko      108:        if(json->key == NULL){
1.59      moko      109:                top->add(value);
1.1       misha     110:        } else {
1.4       moko      111:                switch (json->distinct){
                    112:                        case Json::D_EXCEPTION:
                    113:                                if (top->hash().put_dont_replace(*json->key, value))
                    114:                                        throw Exception(PARSER_RUNTIME, json->key, "duplicate key");
                    115:                                break;
                    116:                        case Json::D_FIRST:
                    117:                                top->hash().put_dont_replace(*json->key, value);
                    118:                                break;
                    119:                        case Json::D_LAST:
                    120:                                top->hash().put(*json->key, value);
                    121:                                break;
                    122:                        case Json::D_ALL:
                    123:                                if (top->hash().put_dont_replace(*json->key, value)){
                    124:                                        for(int i=2;;i++){
                    125:                                                String key;
1.62      moko      126:                                                key << *json->key << "_" << pa_uitoa(i);
1.4       moko      127:                                                if (!top->hash().put_dont_replace(key, value)) break;
                    128:                                        }
                    129:                                }
                    130:                                break;
                    131:                }
1.3       moko      132:                json->key=NULL;
1.1       misha     133:        }
                    134: }
                    135: 
1.25      moko      136: String* json_string(Json *json, const char *value, uint32_t length){
1.3       moko      137:        String::C result = json->charset !=NULL ? 
1.44      moko      138:                Charset::transcode(String::C(value, length), pa_UTF8_charset, *json->charset) :
1.25      moko      139:                String::C(pa_strdup(value, length), length);
1.39      moko      140:        return new String(result, json->taint);
1.1       misha     141: }
                    142: 
1.3       moko      143: static Value *json_hook(Request &r, Junction *hook, String* key, Value* value){
1.10      moko      144:        Value *params[]={new VString(key ? *key : String::Empty), value};
1.49      moko      145:        METHOD_FRAME_ACTION(*hook->method, r.method_frame, hook->self, {
                    146:                frame.store_params(params, 2);
                    147:                r.call(frame);
                    148:                return &frame.result();
                    149:        });
1.1       misha     150: }
                    151: 
1.25      moko      152: static int json_callback(Json *json, int type, const char *value, uint32_t length)
1.1       misha     153: {
                    154:        switch(type) {
1.25      moko      155:                case JSON_OBJECT_BEGIN:{
1.4       moko      156:                        VHash *v = new VHash();
1.16      misha     157:                        if (json->hook_object){
1.1       misha     158:                                json->key_stack.push(json->key);
1.16      misha     159:                                json->key=NULL;
1.1       misha     160:                        } else {
                    161:                                if (json->stack.count()) set_json_value(json, v);
                    162:                        }
                    163:                        json->stack.push(v);
                    164:                        break;
                    165:                }
1.25      moko      166:                case JSON_OBJECT_END:{
1.16      misha     167:                        if (json->hook_object){
1.3       moko      168:                                String* key = json->key_stack.pop();
1.16      misha     169:                                json->result = json_hook(*json->request, json->hook_object, key, json->stack.pop());
1.1       misha     170:                                
                    171:                                if (json->stack.count()){
                    172:                                        json->key = key;
                    173:                                        set_json_value(json, json->result);
                    174:                                }
                    175:                        } else {
                    176:                                json->result = json->stack.pop();
                    177:                        }
                    178:                        break;
                    179:                }
1.25      moko      180:                case JSON_ARRAY_BEGIN:{
1.16      misha     181:                        if (json->hook_array){
                    182:                                json->key_stack.push(json->key);
                    183:                                json->key=NULL;
1.59      moko      184:                                json->stack.push(new VHash);
1.16      misha     185:                        } else {
1.60      moko      186:                                VHashBase *v = json->handle_array ? (VHashBase *)new VArray : (VHashBase *)new VHash;
1.16      misha     187:                                if (json->stack.count()) set_json_value(json, v);
1.59      moko      188:                                json->stack.push(v);
1.16      misha     189:                        }
1.1       misha     190:                        break;
                    191:                }
1.25      moko      192:                case JSON_ARRAY_END:
1.12      moko      193:                        // libjson supports array at top level, we too
1.16      misha     194:                        if (json->hook_array){
                    195:                                String* key = json->key_stack.pop();
                    196:                                json->result = json_hook(*json->request, json->hook_array, key, json->stack.pop());
                    197:                                
                    198:                                if (json->stack.count()){
                    199:                                        json->key = key;
                    200:                                        set_json_value(json, json->result);
                    201:                                }
                    202:                        } else {
                    203:                                json->result = json->stack.pop();
                    204:                        }
1.1       misha     205:                        break;
1.25      moko      206:                case JSON_KEY:
                    207:                        json->key = json_string(json, value, length);
1.16      misha     208:                        break;
1.25      moko      209:                case JSON_INT:
1.30      misha     210:                        if (json->handle_int){
                    211:                                set_json_value(json, new VDouble( json_string(json, value, length)->as_double() ));
                    212:                        } else {
                    213:                                // JSON_STRING
                    214:                                set_json_value(json, new VString(*json_string(json, value, length)));
                    215:                        }
1.1       misha     216:                        break;
1.25      moko      217:                case JSON_FLOAT:
1.1       misha     218:                        if (json->handle_double){
1.25      moko      219:                                set_json_value(json, new VDouble( json_string(json, value, length)->as_double() ));
1.1       misha     220:                                break;
1.25      moko      221:                        } // else is JSON_STRING
                    222:                case JSON_STRING:
                    223:                        set_json_value(json, new VString(*json_string(json, value, length)));
1.1       misha     224:                        break;
1.25      moko      225:                case JSON_NULL:
1.18      moko      226:                        set_json_value(json, VVoid::get());
1.1       misha     227:                        break;
1.25      moko      228:                case JSON_TRUE:
1.1       misha     229:                        set_json_value(json, &VBool::get(true));
                    230:                        break;
1.25      moko      231:                case JSON_FALSE:
1.1       misha     232:                        set_json_value(json, &VBool::get(false));
1.25      moko      233:                        break;
1.1       misha     234:        }
1.25      moko      235:        return 0;
1.1       misha     236: }
                    237: 
1.5       moko      238: static const char* json_error_message(int error_code){
                    239:        static const char* error_messages[] = {
1.1       misha     240:                NULL,
1.25      moko      241:                "out of memory",
                    242:                "bad character",
                    243:                "stack empty",
                    244:                "pop unexpected mode",
                    245:                "nesting limit",
                    246:                "data limit",
                    247:                "comment not allowed by config",
1.35      moko      248:                "unexpected character",
1.25      moko      249:                "missing unicode low surrogate",
                    250:                "unexpected unicode low surrogate",
                    251:                "error comma out of structure",
                    252:                "error in a callback"
1.1       misha     253:        };
                    254:        return error_messages[error_code];
                    255: }
                    256: 
1.23      moko      257: extern String::Language get_untaint_lang(const String& lang_name);
                    258: 
1.35      moko      259: #define SOURCE_MAX_LEN 60
                    260: 
                    261: void json_exception_with_source(Request& r, const char* msg, const char* json, int offset){
                    262:        int i;
                    263: 
                    264:        int line=0;
                    265:        int start=0;
                    266:        int end=strlen(json);
                    267: 
                    268:        if(offset>end)
                    269:                offset=end;
                    270: 
                    271:        for(i = 0; i < offset; i++){
                    272:                if(json[i]=='\n'){
                    273:                        line++;
                    274:                }
                    275:        }
                    276: 
                    277:        if(offset > SOURCE_MAX_LEN/2)
                    278:                start = offset - SOURCE_MAX_LEN/2;
                    279: 
                    280:        for(i = offset-1; i>=start; i--){
                    281:                if(json[i]=='\n'){
                    282:                        start=i+1;
                    283:                        break;
                    284:                }
                    285:        }
                    286: 
                    287:        if(start+SOURCE_MAX_LEN < end)
                    288:                end=start+SOURCE_MAX_LEN;
                    289: 
                    290:        for(i = offset+1; i<end; i++){
                    291:                if(json[i]=='\n'){
                    292:                        end=i;
                    293:                        break;
                    294:                }
                    295:        }
                    296: 
                    297:        char *source = pa_strdup(json+start, end-start);
                    298:        int source_offset = offset-start;
                    299: 
                    300:        if(source[source_offset]=='\n')
                    301:                source[source_offset]=' ';
                    302: 
                    303:        for(i = 0; i < source_offset; i++){
                    304:                if(source[i]=='\t'){
                    305:                        source[i]=' ';
                    306:                }
                    307:        }
                    308: 
                    309:        if(r.charsets.source().isUTF8()){
                    310:                source=(char *)fixUTF8(source);
                    311:                if(source_offset>0){
                    312:                        String s_source(pa_strdup(source,source_offset));
                    313:                        source_offset=s_source.length(r.charsets.source());
                    314:                }
                    315:        }
                    316: 
                    317:        throw Exception("json.parse", 0, "%s at line %d\n%s\n%*s", msg, line+1, source, source_offset+1, "^");
                    318: }
                    319: 
1.1       misha     320: static void _parse(Request& r, MethodParams& params) {
1.3       moko      321:        const String& json_string=params.as_string(0, "json must be string");
                    322: 
                    323:        Json json(r.charsets.source().isUTF8() ? NULL : &(r.charsets.source()));
1.1       misha     324: 
1.25      moko      325:        json_config config = {
                    326:                0,              // buffer_initial_size
1.26      moko      327:                128,            // max_nesting
1.25      moko      328:                0,              // max_data
                    329:                1,              // allow_c_comments
                    330:                1,              // allow_yaml_comments
                    331:                pa_malloc,
                    332:                pa_realloc,
                    333:                pa_free
                    334:        };
1.1       misha     335: 
                    336:        if(params.count() == 2)
                    337:                if(HashStringValue* options=params.as_hash(1)) {
                    338:                        int valid_options=0;
                    339:                        if(Value* value=options->get("depth")) {
1.46      moko      340:                                config.max_nesting=r.process(*value).as_int();
1.1       misha     341:                                valid_options++;
                    342:                        }
                    343:                        if(Value* value=options->get("double")) {
1.46      moko      344:                                json.handle_double=r.process(*value).as_bool();
1.4       moko      345:                                valid_options++;
                    346:                        }
1.30      misha     347:                        if(Value* value=options->get("int")) {
1.46      moko      348:                                json.handle_int=r.process(*value).as_bool();
1.30      misha     349:                                valid_options++;
                    350:                        }
1.4       moko      351:                        if(Value* value=options->get("distinct")) {
                    352:                                const String& sdistinct=value->as_string();
                    353:                                if (!json.set_distinct(sdistinct))
                    354:                                        throw Exception(PARSER_RUNTIME, &sdistinct, "must be 'first', 'last' or 'all'");
1.1       misha     355:                                valid_options++;
                    356:                        }
1.23      moko      357:                        if(Value* value=options->get("taint")) {
                    358:                                json.taint=get_untaint_lang(value->as_string());
                    359:                                valid_options++;
                    360:                        }
1.1       misha     361:                        if(Value* value=options->get("object")) {
1.16      misha     362:                                json.hook_object=value->get_junction();
1.3       moko      363:                                json.request=&r;
1.49      moko      364:                                if (!json.hook_object || !json.hook_object->method || !json.hook_object->method->params_names || !(json.hook_object->method->params_count == 2))
1.1       misha     365:                                        throw Exception(PARSER_RUNTIME, 0, "$.object must be parser method with 2 parameters");
                    366:                                valid_options++;
                    367:                        }
1.16      misha     368:                        if(Value* value=options->get("array")) {
1.59      moko      369:                                if(value->get_string()){
                    370:                                        const String& sarray=value->as_string();
                    371:                                        if (!json.set_handle_array(sarray))
                    372:                                                throw Exception(PARSER_RUNTIME, &sarray, "$.array must be parser method with 2 parameters or 'array' or 'hash'");
                    373:                                } else {
                    374:                                        json.hook_array=value->get_junction();
                    375:                                        json.request=&r;
                    376:                                        if (!json.hook_array || !json.hook_array->method || !json.hook_array->method->params_names || !(json.hook_array->method->params_count == 2))
                    377:                                                throw Exception(PARSER_RUNTIME, 0, "$.array must be parser method with 2 parameters or 'array' or 'hash'");
                    378:                                }
1.16      misha     379:                                valid_options++;
                    380:                        }
1.1       misha     381:                        if(valid_options!=options->count())
                    382:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    383:                }
                    384: 
1.28      moko      385:        const String::Body json_body = json_string.cstr_to_string_body_untaint(String::L_JSON, r.connection(false), &r.charsets);
1.44      moko      386:        const char *json_cstr = json.charset != NULL ? Charset::transcode(json_body, *json.charset, pa_UTF8_charset).cstr() : json_body.cstr();
1.1       misha     387: 
1.25      moko      388:        json_parser parser;
                    389:        if(int result = json_parser_init(&parser, &config, (json_parser_callback)&json_callback, &json))
                    390:                throw Exception("json.parse", 0, "%s", json_error_message(result));
                    391: 
1.36      moko      392:        if(!*json_cstr)
                    393:                throw Exception("json.parse", 0, "empty string is not valid json");
                    394: 
                    395:        const char *first_quote=strchr(json_cstr,'"');
                    396:        if(first_quote && first_quote>json_cstr && *(--first_quote) == '\\')
                    397:                json_exception_with_source(r, "illegal quote escape, json may be tainted", json_cstr, first_quote-json_cstr);
                    398: 
1.25      moko      399:        uint32_t processed;
                    400:        if(int result = json_parser_string(&parser, json_cstr, strlen(json_cstr), &processed))
1.35      moko      401:                json_exception_with_source(r, json_error_message(result), json_cstr, processed);
1.3       moko      402: 
1.25      moko      403:        if (!json_parser_is_done(&parser))
1.35      moko      404:                json_exception_with_source(r, "unexpected end of json data", json_cstr, processed);
                    405: 
1.25      moko      406:        json_parser_free(&parser);
1.1       misha     407: 
1.48      moko      408:        if (json.result) r.write(*json.result);
1.1       misha     409: }
                    410: 
1.63      moko      411: const uint ANTI_ENDLESS_JSON_STRING_RECURSION=128;
1.26      moko      412: 
1.8       moko      413: char *get_indent(uint level){
1.63      moko      414:        static char* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={};
1.8       moko      415:        if (!cache[level]){
1.56      moko      416:                char *result = static_cast<char*>(pa_malloc_atomic(level+1));
1.8       moko      417:                memset(result, '\t', level);
1.9       moko      418:                result[level]='\0';
1.8       moko      419:                return cache[level]=result;
                    420:        }
                    421:        return cache[level];
                    422: }
                    423: 
1.56      moko      424: String *get_delim(uint level){
1.63      moko      425:        static String* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={};
1.56      moko      426: 
                    427:        if (!cache[level]){
                    428:                char *result = static_cast<char*>(pa_malloc_atomic(level+2+1+1));
                    429:                result[0]=',';
                    430:                result[1]='\n';
                    431:                memset(result+2, '\t', level);
                    432:                result[level+2]='"';
                    433:                result[level+3]='\0';
                    434:                return cache[level] = new String(result, String::L_AS_IS);
                    435:        }
                    436:        return cache[level];
                    437: }
                    438: 
1.59      moko      439: String *get_array_delim(uint level){
1.63      moko      440:        static String* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={};
1.59      moko      441: 
                    442:        if (!cache[level]){
                    443:                char *result = static_cast<char*>(pa_malloc_atomic(level+2+1));
                    444:                result[0]=',';
                    445:                result[1]='\n';
                    446:                memset(result+2, '\t', level);
                    447:                result[level+2]='\0';
                    448:                return cache[level] = new String(result, String::L_AS_IS);
                    449:        }
                    450:        return cache[level];
                    451: }
                    452: 
1.63      moko      453: class Json_string_recursion {
1.26      moko      454:        Json_options& foptions;
                    455: public:
1.63      moko      456:        Json_string_recursion(Json_options& aoptions) : foptions(aoptions) {
                    457:                if(++foptions.json_string_recursion==ANTI_ENDLESS_JSON_STRING_RECURSION)
1.26      moko      458:                        throw Exception(PARSER_RUNTIME, 0, "call canceled - endless json recursion detected");
                    459:        }
1.63      moko      460:        ~Json_string_recursion() {
                    461:                if(foptions.json_string_recursion)
                    462:                        foptions.json_string_recursion--;
1.26      moko      463:        }
                    464: };
                    465: 
1.21      moko      466: const String& value_json_string(String::Body key, Value& v, Json_options& options);
1.6       misha     467: 
1.37      moko      468: const String* Json_options::hash_json_string(HashStringValue *hash) {
                    469:        if(!hash || !hash->count())
1.21      moko      470:                return new String("{}", String::L_AS_IS);
1.8       moko      471: 
1.63      moko      472:        Json_string_recursion go_down(*this);
1.8       moko      473: 
                    474:        String& result = *new String("{\n", String::L_AS_IS);
                    475: 
1.21      moko      476:        if (indent){
1.8       moko      477: 
                    478:                String *delim=NULL;
1.63      moko      479:                indent=get_indent(json_string_recursion);
1.37      moko      480:                for(HashStringValue::Iterator i(*hash); i; i.next() ){
1.8       moko      481:                        if (delim){
                    482:                                result << *delim;
                    483:                        } else {
1.21      moko      484:                                result << indent << "\"";
1.63      moko      485:                                delim = get_delim(json_string_recursion);
1.8       moko      486:                        }
1.21      moko      487:                        result << String(i.key(), String::L_JSON) << "\":" << value_json_string(i.key(), *i.value(), *this);
1.8       moko      488:                }
1.63      moko      489:                result << "\n" << (indent=get_indent(json_string_recursion-1)) << "}";
1.6       misha     490: 
1.8       moko      491:        } else {
                    492: 
                    493:                bool need_delim=false;
1.37      moko      494:                for(HashStringValue::Iterator i(*hash); i; i.next() ){
1.8       moko      495:                        result << (need_delim ? ",\n\"" : "\"");
1.21      moko      496:                        result << String(i.key(), String::L_JSON) << "\":" << value_json_string(i.key(), *i.value(), *this);
1.8       moko      497:                        need_delim=true;
                    498:                }
                    499:                result << "\n}";
1.6       misha     500: 
                    501:        }
                    502: 
1.21      moko      503:        return &result;
1.6       misha     504: }
                    505: 
1.59      moko      506: const String* Json_options::array_json_string(ArrayValue *array) {
                    507:        if(!array || !array->count())
                    508:                return new String("[]", String::L_AS_IS);
                    509: 
1.63      moko      510:        Json_string_recursion go_down(*this);
1.59      moko      511: 
                    512:        String& result = *new String("[\n", String::L_AS_IS);
                    513: 
                    514:        if (indent){
                    515: 
                    516:                String *delim=NULL;
1.63      moko      517:                indent=get_indent(json_string_recursion);
1.59      moko      518:                for(ArrayValue::Iterator i(*array); i; i.next() ){
                    519:                        if (delim){
                    520:                                result << *delim;
                    521:                        } else {
                    522:                                result << indent;
1.63      moko      523:                                delim = get_array_delim(json_string_recursion);
1.59      moko      524:                        }
1.65      moko      525:                        result << value_json_string(i.key(), i.value() ? *i.value() : static_cast<Value&>(*VVoid::get()), *this);
1.59      moko      526:                }
1.63      moko      527:                result << "\n" << (indent=get_indent(json_string_recursion-1)) << "]";
1.59      moko      528: 
                    529:        } else {
                    530: 
                    531:                bool need_delim=false;
                    532:                for(ArrayValue::Iterator i(*array); i; i.next() ){
                    533:                        if(need_delim) result << ",\n";
1.65      moko      534:                        result << value_json_string(i.key(), i.value() ? *i.value() : static_cast<Value&>(*VVoid::get()), *this);
1.59      moko      535:                        need_delim=true;
                    536:                }
                    537:                result << "\n]";
                    538: 
                    539:        }
                    540: 
                    541:        return &result;
                    542: }
                    543: 
                    544: const String* Json_options::array_compact_json_string(ArrayValue *array) {
                    545:        if(!array || !array->count())
                    546:                return new String("[]", String::L_AS_IS);
                    547: 
1.63      moko      548:        Json_string_recursion go_down(*this);
1.59      moko      549: 
                    550:        String& result = *new String("[\n", String::L_AS_IS);
                    551: 
                    552:        if (indent){
                    553: 
                    554:                String *delim=NULL;
1.63      moko      555:                indent=get_indent(json_string_recursion);
1.59      moko      556:                for(ArrayValue::Iterator i(*array); i; i.next() ){
                    557:                        if (i.value()){
                    558:                                if (delim){
                    559:                                        result << *delim;
                    560:                                } else {
                    561:                                        result << indent;
1.63      moko      562:                                        delim = get_array_delim(json_string_recursion);
1.59      moko      563:                                }
                    564:                                result << value_json_string(i.key(), *i.value(), *this);
                    565:                        }
                    566:                }
1.63      moko      567:                result << "\n" << (indent=get_indent(json_string_recursion-1)) << "]";
1.59      moko      568: 
                    569:        } else {
                    570: 
                    571:                bool need_delim=false;
                    572:                for(ArrayValue::Iterator i(*array); i; i.next() ){
                    573:                        if (i.value()){
                    574:                                if(need_delim) result << ",\n";
                    575:                                result << value_json_string(i.key(), *i.value(), *this);
                    576:                                need_delim=true;
                    577:                        }
                    578:                }
                    579:                result << "\n]";
                    580: 
                    581:        }
                    582: 
                    583:        return &result;
                    584: }
                    585: 
1.21      moko      586: static bool based_on(HashStringValue::key_type key, HashStringValue::value_type /*value*/, Value* v) {
1.15      misha     587:        return v->is(key.cstr());
                    588: }
1.26      moko      589: 
1.21      moko      590: const String& value_json_string(String::Body key, Value& v, Json_options& options) {
                    591:        if(options.methods) {
                    592:                Value* method=options.methods->get(v.type());
                    593:                if(!method){
                    594:                        method=options.methods->first_that<Value*>(based_on, &v);
1.31      misha     595:                        options.methods->put(v.type(), method ? method : VVoid::get());
1.21      moko      596:                }
                    597:                if(method && !method->is_void()) {
1.6       misha     598:                        Junction* junction=method->get_junction();
1.26      moko      599:                        HashStringValue* params_hash=options.params && options.indent ? options.params->get_hash() : NULL;
1.27      moko      600:                        Temp_hash_value<HashStringValue, Value*> indent(params_hash, "indent", new VString(*new String(options.indent, String::L_AS_IS)));
1.26      moko      601: 
1.21      moko      602:                        Value *params[]={new VString(*new String(key, String::L_JSON)), &v, options.params ? options.params : VVoid::get()};
1.6       misha     603: 
1.49      moko      604:                        METHOD_FRAME_ACTION(*junction->method, options.r->method_frame, junction->self, {
                    605:                                frame.store_params(params, 3);
                    606:                                options.r->call(frame);
                    607:                                return frame.result().as_string();
                    608:                        });
1.6       misha     609:                }
1.15      misha     610:        }
1.6       misha     611: 
1.21      moko      612:        options.key=key;
1.6       misha     613:        return *v.get_json_string(options);
                    614: }
                    615: 
                    616: static void _string(Request& r, MethodParams& params) {
                    617:        Json_options json(&r);
                    618: 
                    619:        if(params.count() == 2)
                    620:                if(HashStringValue* options=params.as_hash(1)) {
1.47      moko      621:                        json.params=&params[1];
1.6       misha     622:                        HashStringValue* methods=new HashStringValue();
                    623:                        int valid_options=0;
1.14      misha     624:                        HashStringValue* vvalue;
1.6       misha     625:                        for(HashStringValue::Iterator i(*options); i; i.next() ){
                    626:                                String::Body key=i.key();
                    627:                                Value* value=i.value();
                    628:                                if(key == "skip-unknown"){
1.46      moko      629:                                        json.skip_unknown=r.process(*value).as_bool();
1.6       misha     630:                                        valid_options++;
1.50      moko      631:                                } else if(key == "one-line"){
                    632:                                        json.one_line=r.process(*value).as_bool();
                    633:                                        valid_options++;
1.6       misha     634:                                } else if(key == "date" && value->is_string()){
                    635:                                        const String& svalue=value->as_string();
                    636:                                        if(!json.set_date_format(svalue))
1.38      moko      637:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'sql-string', 'gmt-string', 'iso-string' or 'unix-timestamp'");
1.6       misha     638:                                        valid_options++;
1.8       moko      639:                                } else if(key == "indent"){
1.26      moko      640:                                        if(value->is_string()){
                    641:                                                json.indent=value->as_string().cstr();
1.63      moko      642:                                                json.json_string_recursion=strlen(json.indent);
1.46      moko      643:                                        } else json.indent=r.process(*value).as_bool() ? "" : NULL;
1.8       moko      644:                                        valid_options++;
1.6       misha     645:                                } else if(key == "table" && value->is_string()){
                    646:                                        const String& svalue=value->as_string();
                    647:                                        if(!json.set_table_format(svalue))
1.13      moko      648:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'array', 'object' or 'compact'");
1.6       misha     649:                                        valid_options++;
1.59      moko      650:                                } else if(key == "array" && value->is_string()){
                    651:                                        const String& svalue=value->as_string();
                    652:                                        if(!json.set_array_format(svalue))
                    653:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'array', 'object' or 'compact'");
                    654:                                        valid_options++;
1.6       misha     655:                                } else if(key == "file" && value->is_string()){
                    656:                                        const String& svalue=value->as_string();
                    657:                                        if(!json.set_file_format(svalue))
1.19      misha     658:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'base64', 'text' or 'stat'");
1.6       misha     659:                                        valid_options++;
1.32      misha     660:                                } else if(key == "void" && value->is_string()){
                    661:                                        const String& svalue=value->as_string();
                    662:                                        if(!json.set_void_format(svalue))
                    663:                                                throw Exception(PARSER_RUNTIME, &svalue, "must be 'string' or 'null'");
                    664:                                        valid_options++;
1.14      misha     665: #ifdef XML
                    666:                                } else if(key == "xdoc" && (vvalue = value->get_hash())){
1.24      moko      667:                                        json.xdoc_options=new XDocOutputOptions();
                    668:                                        json.xdoc_options->append(r, vvalue);
1.14      misha     669:                                        valid_options++;
                    670: #endif
1.6       misha     671:                                } else if(Junction* junction=value->get_junction()){
1.49      moko      672:                                        if(!junction->method || !junction->method->params_names || junction->method->params_count != 3)
1.13      moko      673:                                                throw Exception(PARSER_RUNTIME, 0, "$.%s must be parser method with 3 parameters", key.cstr());
1.6       misha     674:                                        methods->put(key, value);
                    675:                                        valid_options++;
                    676:                                }
                    677:                        }
1.22      moko      678: 
1.6       misha     679:                        if(valid_options!=options->count())
                    680:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.22      moko      681: 
                    682:                        // special handling for $._default 
1.61      moko      683:                        if(VHashBase* vhash=dynamic_cast<VHashBase*>(&params[1]))
1.22      moko      684:                                if(Value* value=vhash->get_default()) {
1.34      misha     685:                                        if(!value->is_string()){
1.43      moko      686:                                                Junction* junction=value->get_junction();
1.49      moko      687:                                                if(!junction || !junction->method || !junction->method->params_names || junction->method->params_count != 3)
1.42      moko      688:                                                        throw Exception(PARSER_RUNTIME, 0, "$._default must be string or parser method with 3 parameters");
1.34      misha     689:                                        }
1.22      moko      690:                                        json.default_method=value;
                    691:                                }
                    692: 
1.6       misha     693:                        if(methods->count())
                    694:                                json.methods=methods;
                    695:                }
1.14      misha     696: 
1.46      moko      697:        const String& result_string=value_json_string(String::Body(), r.process(params[0]), json);
1.28      moko      698:        String::Body result_body=result_string.cstr_to_string_body_untaint(String::L_JSON, r.connection(false), &r.charsets);
1.50      moko      699:        if(json.one_line){
                    700:                char *result=result_body.cstrm();
                    701:                for(char *c=result;*c;c++)
                    702:                        if(*c=='\n')
                    703:                                *c=' ';
                    704:                result_body=result;
                    705:        }
1.48      moko      706:        r.write(*new String(result_body, String::L_AS_IS));
1.50      moko      707: }
1.6       misha     708: 
1.1       misha     709: // constructor
                    710: 
                    711: MJson::MJson(): Methoded("json") {
                    712:        add_native_method("parse", Method::CT_STATIC, _parse, 1, 2);
1.6       misha     713: 
1.58      moko      714:        add_native_method("string", Method::CT_STATIC, _string, 1, 2);
1.1       misha     715: }

E-mail: