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

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

E-mail: