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

version 1.60, 2024/09/10 19:15:48 version 1.65, 2024/10/20 14:30:14
Line 27  volatile const char * IDENT_JSON_C="$Id$ Line 27  volatile const char * IDENT_JSON_C="$Id$
 class MJson: public Methoded {  class MJson: public Methoded {
 public:  public:
         MJson();          MJson();
   
           override Value* get_element(const String&);
           override const VJunction* put_element(const String&, Value*);
 };  };
   
 // global variable  // global variable
   
 DECLARE_CLASS_VAR(json, new MJson);  DECLARE_CLASS_VAR(json, new MJson);
   
   bool handle_array_default=true;
   
 // methods  // methods
 struct Json : public PA_Allocated {  struct Json : public PA_Allocated {
         Stack<VHashBase*> stack;          Stack<VHashBase*> stack;
Line 55  struct Json : public PA_Allocated { Line 60  struct Json : public PA_Allocated {
   
         Json(Charset* acharset): stack(), key_stack(), key(NULL), result(NULL), hook_object(NULL), hook_array(NULL),           Json(Charset* acharset): stack(), key_stack(), key(NULL), result(NULL), hook_object(NULL), hook_array(NULL), 
                 request(NULL), charset(acharset), taint(String::L_TAINTED), handle_double(true), handle_int(true),                   request(NULL), charset(acharset), taint(String::L_TAINTED), handle_double(true), handle_int(true), 
                 handle_array(true), distinct(D_EXCEPTION){}                  handle_array(handle_array_default), distinct(D_EXCEPTION){}
   
         bool set_distinct(const String &value){          bool set_distinct(const String &value){
                 if (value == "first") distinct = D_FIRST;                  if (value == "first") distinct = D_FIRST;
Line 73  struct Json : public PA_Allocated { Line 78  struct Json : public PA_Allocated {
         }          }
 };  };
   
   
   Value* MJson::get_element(const String& aname) {
           if (aname=="array"){
                   return new VString(*new String(handle_array_default ? "array" : "hash"));
           }
           return Methoded::get_element(aname);
   }
   
   const VJunction* MJson::put_element(const String& aname, Value* avalue) {
           if (aname=="array"){
                   Json json(NULL);
                   if (avalue->get_string()){
                           const String& sarray=avalue->as_string();
                           if (json.set_handle_array(sarray)){
                                   handle_array_default=json.handle_array;
                                   return 0;
                           }
                           throw Exception(PARSER_RUNTIME, &sarray, "$json:array must be 'array' or 'hash'");
                   }
                   throw Exception(PARSER_RUNTIME, 0, "$json:array must be 'array' or 'hash'");
           }
           return Methoded::put_element(aname, avalue);
   }
   
   
 static void set_json_value(Json *json, Value *value){  static void set_json_value(Json *json, Value *value){
         VHashBase *top = json->stack.top_value();          VHashBase *top = json->stack.top_value();
         if(json->key == NULL){          if(json->key == NULL){
Line 93  static void set_json_value(Json *json, V Line 123  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 408  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 422  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 437  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 450  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 469  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 507  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() : static_cast<Value&>(*VVoid::get()), *this);
                 }                  }
                 result << "\n" << (indent=get_indent(json_string_recoursion-1)) << "]";                  result << "\n" << (indent=get_indent(json_string_recursion-1)) << "]";
   
         } else {          } else {
   
                 bool need_delim=false;                  bool need_delim=false;
                 for(ArrayValue::Iterator i(*array); i; i.next() ){                  for(ArrayValue::Iterator i(*array); i; i.next() ){
                         if(need_delim) result << ",\n";                          if(need_delim) result << ",\n";
                         result << value_json_string(i.key(), i.value() ? *i.value() : *VVoid::get(), *this);                          result << value_json_string(i.key(), i.value() ? *i.value() : static_cast<Value&>(*VVoid::get()), *this);
                         need_delim=true;                          need_delim=true;
                 }                  }
                 result << "\n]";                  result << "\n]";
Line 515  const String* Json_options::array_compac Line 545  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 639  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 680  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.65


E-mail: