--- parser3/src/classes/json.C 2024/09/10 19:09:56 1.59 +++ parser3/src/classes/json.C 2024/10/02 21:24:41 1.63 @@ -10,7 +10,7 @@ #include "pa_request.h" #include "pa_vbool.h" -//#include "pa_varray.h" +#include "pa_varray.h" #include "pa_charset.h" #include "pa_charsets.h" @@ -20,7 +20,7 @@ #include "pa_vxdoc.h" #endif -volatile const char * IDENT_JSON_C="$Id: json.C,v 1.59 2024/09/10 19:09:56 moko Exp $"; +volatile const char * IDENT_JSON_C="$Id: json.C,v 1.63 2024/10/02 21:24:41 moko Exp $"; // class @@ -93,7 +93,7 @@ static void set_json_value(Json *json, V if (top->hash().put_dont_replace(*json->key, value)){ for(int i=2;;i++){ String key; - key << *json->key << "_" << format(i, 0); + key << *json->key << "_" << pa_uitoa(i); if (!top->hash().put_dont_replace(key, value)) break; } } @@ -153,8 +153,7 @@ static int json_callback(Json *json, int json->key=NULL; json->stack.push(new VHash); } else { - VHashBase *v = new VHash; -// VHashBase *v = json->handle_array ? (VHashBase *)new VArray : (VHashBase *)new VHash; + VHashBase *v = json->handle_array ? (VHashBase *)new VArray : (VHashBase *)new VHash; if (json->stack.count()) set_json_value(json, v); json->stack.push(v); } @@ -379,10 +378,10 @@ static void _parse(Request& r, MethodPar if (json.result) r.write(*json.result); } -const uint ANTI_ENDLESS_JSON_STRING_RECOURSION=128; +const uint ANTI_ENDLESS_JSON_STRING_RECURSION=128; char *get_indent(uint level){ - static char* cache[ANTI_ENDLESS_JSON_STRING_RECOURSION]={}; + static char* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={}; if (!cache[level]){ char *result = static_cast(pa_malloc_atomic(level+1)); memset(result, '\t', level); @@ -393,7 +392,7 @@ char *get_indent(uint level){ } String *get_delim(uint level){ - static String* cache[ANTI_ENDLESS_JSON_STRING_RECOURSION]={}; + static String* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={}; if (!cache[level]){ char *result = static_cast(pa_malloc_atomic(level+2+1+1)); @@ -408,7 +407,7 @@ String *get_delim(uint level){ } String *get_array_delim(uint level){ - static String* cache[ANTI_ENDLESS_JSON_STRING_RECOURSION]={}; + static String* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={}; if (!cache[level]){ char *result = static_cast(pa_malloc_atomic(level+2+1)); @@ -421,16 +420,16 @@ String *get_array_delim(uint level){ return cache[level]; } -class Json_string_recoursion { +class Json_string_recursion { Json_options& foptions; public: - Json_string_recoursion(Json_options& aoptions) : foptions(aoptions) { - if(++foptions.json_string_recoursion==ANTI_ENDLESS_JSON_STRING_RECOURSION) + Json_string_recursion(Json_options& aoptions) : foptions(aoptions) { + if(++foptions.json_string_recursion==ANTI_ENDLESS_JSON_STRING_RECURSION) throw Exception(PARSER_RUNTIME, 0, "call canceled - endless json recursion detected"); } - ~Json_string_recoursion() { - if(foptions.json_string_recoursion) - foptions.json_string_recoursion--; + ~Json_string_recursion() { + if(foptions.json_string_recursion) + foptions.json_string_recursion--; } }; @@ -440,24 +439,24 @@ const String* Json_options::hash_json_st if(!hash || !hash->count()) return new String("{}", String::L_AS_IS); - Json_string_recoursion go_down(*this); + Json_string_recursion go_down(*this); String& result = *new String("{\n", String::L_AS_IS); if (indent){ String *delim=NULL; - indent=get_indent(json_string_recoursion); + indent=get_indent(json_string_recursion); for(HashStringValue::Iterator i(*hash); i; i.next() ){ if (delim){ result << *delim; } else { result << indent << "\""; - delim = get_delim(json_string_recoursion); + delim = get_delim(json_string_recursion); } result << String(i.key(), String::L_JSON) << "\":" << value_json_string(i.key(), *i.value(), *this); } - result << "\n" << (indent=get_indent(json_string_recoursion-1)) << "}"; + result << "\n" << (indent=get_indent(json_string_recursion-1)) << "}"; } else { @@ -478,24 +477,24 @@ const String* Json_options::array_json_s if(!array || !array->count()) return new String("[]", String::L_AS_IS); - Json_string_recoursion go_down(*this); + Json_string_recursion go_down(*this); String& result = *new String("[\n", String::L_AS_IS); if (indent){ String *delim=NULL; - indent=get_indent(json_string_recoursion); + indent=get_indent(json_string_recursion); for(ArrayValue::Iterator i(*array); i; i.next() ){ if (delim){ result << *delim; } else { result << indent; - delim = get_array_delim(json_string_recoursion); + delim = get_array_delim(json_string_recursion); } result << value_json_string(i.key(), i.value() ? *i.value() : *VVoid::get(), *this); } - result << "\n" << (indent=get_indent(json_string_recoursion-1)) << "]"; + result << "\n" << (indent=get_indent(json_string_recursion-1)) << "]"; } else { @@ -516,26 +515,26 @@ const String* Json_options::array_compac if(!array || !array->count()) return new String("[]", String::L_AS_IS); - Json_string_recoursion go_down(*this); + Json_string_recursion go_down(*this); String& result = *new String("[\n", String::L_AS_IS); if (indent){ String *delim=NULL; - indent=get_indent(json_string_recoursion); + indent=get_indent(json_string_recursion); for(ArrayValue::Iterator i(*array); i; i.next() ){ if (i.value()){ if (delim){ result << *delim; } else { result << indent; - delim = get_array_delim(json_string_recoursion); + delim = get_array_delim(json_string_recursion); } result << value_json_string(i.key(), *i.value(), *this); } } - result << "\n" << (indent=get_indent(json_string_recoursion-1)) << "]"; + result << "\n" << (indent=get_indent(json_string_recursion-1)) << "]"; } else { @@ -610,7 +609,7 @@ static void _string(Request& r, MethodPa } else if(key == "indent"){ if(value->is_string()){ json.indent=value->as_string().cstr(); - json.json_string_recoursion=strlen(json.indent); + json.json_string_recursion=strlen(json.indent); } else json.indent=r.process(*value).as_bool() ? "" : NULL; valid_options++; } else if(key == "table" && value->is_string()){ @@ -651,7 +650,7 @@ static void _string(Request& r, MethodPa throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); // special handling for $._default - if(VHashBase* vhash=static_cast(params[1].as(VHASH_TYPE))) + if(VHashBase* vhash=dynamic_cast(¶ms[1])) if(Value* value=vhash->get_default()) { if(!value->is_string()){ Junction* junction=value->get_junction();