Diff for /parser3/src/classes/json.C between versions 1.60 and 1.63

version 1.60, 2024/09/10 19:15:48 version 1.63, 2024/10/02 21:24:41
Line 93  static void set_json_value(Json *json, V Line 93  static void set_json_value(Json *json, V
                                 if (top->hash().put_dont_replace(*json->key, value)){                                  if (top->hash().put_dont_replace(*json->key, value)){
                                         for(int i=2;;i++){                                          for(int i=2;;i++){
                                                 String key;                                                  String key;
                                                 key << *json->key << "_" << format(i, 0);                                                  key << *json->key << "_" << pa_uitoa(i);
                                                 if (!top->hash().put_dont_replace(key, value)) break;                                                  if (!top->hash().put_dont_replace(key, value)) break;
                                         }                                          }
                                 }                                  }
Line 378  static void _parse(Request& r, MethodPar Line 378  static void _parse(Request& r, MethodPar
         if (json.result) r.write(*json.result);          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){  char *get_indent(uint level){
         static char* cache[ANTI_ENDLESS_JSON_STRING_RECOURSION]={};          static char* cache[ANTI_ENDLESS_JSON_STRING_RECURSION]={};
         if (!cache[level]){          if (!cache[level]){
                 char *result = static_cast<char*>(pa_malloc_atomic(level+1));                  char *result = static_cast<char*>(pa_malloc_atomic(level+1));
                 memset(result, '\t', level);                  memset(result, '\t', level);
Line 392  char *get_indent(uint level){ Line 392  char *get_indent(uint level){
 }  }
   
 String *get_delim(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]){          if (!cache[level]){
                 char *result = static_cast<char*>(pa_malloc_atomic(level+2+1+1));                  char *result = static_cast<char*>(pa_malloc_atomic(level+2+1+1));
Line 407  String *get_delim(uint level){ Line 407  String *get_delim(uint level){
 }  }
   
 String *get_array_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]){          if (!cache[level]){
                 char *result = static_cast<char*>(pa_malloc_atomic(level+2+1));                  char *result = static_cast<char*>(pa_malloc_atomic(level+2+1));
Line 420  String *get_array_delim(uint level){ Line 420  String *get_array_delim(uint level){
         return cache[level];          return cache[level];
 }  }
   
 class Json_string_recoursion {  class Json_string_recursion {
         Json_options& foptions;          Json_options& foptions;
 public:  public:
         Json_string_recoursion(Json_options& aoptions) : foptions(aoptions) {          Json_string_recursion(Json_options& aoptions) : foptions(aoptions) {
                 if(++foptions.json_string_recoursion==ANTI_ENDLESS_JSON_STRING_RECOURSION)                  if(++foptions.json_string_recursion==ANTI_ENDLESS_JSON_STRING_RECURSION)
                         throw Exception(PARSER_RUNTIME, 0, "call canceled - endless json recursion detected");                          throw Exception(PARSER_RUNTIME, 0, "call canceled - endless json recursion detected");
         }          }
         ~Json_string_recoursion() {          ~Json_string_recursion() {
                 if(foptions.json_string_recoursion)                  if(foptions.json_string_recursion)
                         foptions.json_string_recoursion--;                          foptions.json_string_recursion--;
         }          }
 };  };
   
Line 439  const String* Json_options::hash_json_st Line 439  const String* Json_options::hash_json_st
         if(!hash || !hash->count())          if(!hash || !hash->count())
                 return new String("{}", String::L_AS_IS);                  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);          String& result = *new String("{\n", String::L_AS_IS);
   
         if (indent){          if (indent){
   
                 String *delim=NULL;                  String *delim=NULL;
                 indent=get_indent(json_string_recoursion);                  indent=get_indent(json_string_recursion);
                 for(HashStringValue::Iterator i(*hash); i; i.next() ){                  for(HashStringValue::Iterator i(*hash); i; i.next() ){
                         if (delim){                          if (delim){
                                 result << *delim;                                  result << *delim;
                         } else {                          } else {
                                 result << indent << "\"";                                  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 << 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 {          } else {
   
Line 477  const String* Json_options::array_json_s Line 477  const String* Json_options::array_json_s
         if(!array || !array->count())          if(!array || !array->count())
                 return new String("[]", String::L_AS_IS);                  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);          String& result = *new String("[\n", String::L_AS_IS);
   
         if (indent){          if (indent){
   
                 String *delim=NULL;                  String *delim=NULL;
                 indent=get_indent(json_string_recoursion);                  indent=get_indent(json_string_recursion);
                 for(ArrayValue::Iterator i(*array); i; i.next() ){                  for(ArrayValue::Iterator i(*array); i; i.next() ){
                         if (delim){                          if (delim){
                                 result << *delim;                                  result << *delim;
                         } else {                          } else {
                                 result << indent;                                  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 << 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 {          } else {
   
Line 515  const String* Json_options::array_compac Line 515  const String* Json_options::array_compac
         if(!array || !array->count())          if(!array || !array->count())
                 return new String("[]", String::L_AS_IS);                  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);          String& result = *new String("[\n", String::L_AS_IS);
   
         if (indent){          if (indent){
   
                 String *delim=NULL;                  String *delim=NULL;
                 indent=get_indent(json_string_recoursion);                  indent=get_indent(json_string_recursion);
                 for(ArrayValue::Iterator i(*array); i; i.next() ){                  for(ArrayValue::Iterator i(*array); i; i.next() ){
                         if (i.value()){                          if (i.value()){
                                 if (delim){                                  if (delim){
                                         result << *delim;                                          result << *delim;
                                 } else {                                  } else {
                                         result << indent;                                          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 << 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 {          } else {
   
Line 609  static void _string(Request& r, MethodPa Line 609  static void _string(Request& r, MethodPa
                                 } else if(key == "indent"){                                  } else if(key == "indent"){
                                         if(value->is_string()){                                          if(value->is_string()){
                                                 json.indent=value->as_string().cstr();                                                  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;                                          } else json.indent=r.process(*value).as_bool() ? "" : NULL;
                                         valid_options++;                                          valid_options++;
                                 } else if(key == "table" && value->is_string()){                                  } else if(key == "table" && value->is_string()){
Line 650  static void _string(Request& r, MethodPa Line 650  static void _string(Request& r, MethodPa
                                 throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);                                  throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
   
                         // special handling for $._default                           // special handling for $._default 
                         if(VHashBase* vhash=static_cast<VHashBase*>(params[1].as(VHASH_TYPE)))                          if(VHashBase* vhash=dynamic_cast<VHashBase*>(&params[1]))
                                 if(Value* value=vhash->get_default()) {                                  if(Value* value=vhash->get_default()) {
                                         if(!value->is_string()){                                          if(!value->is_string()){
                                                 Junction* junction=value->get_junction();                                                  Junction* junction=value->get_junction();

Removed from v.1.60  
changed lines
  Added in v.1.63


E-mail: