C + + Jsoncpp parse for JSON string parsing and conversion to complement JSON format validation

Source: Internet
Author: User

Recently, when using jsoncpp, it is necessary to determine whether the current string is the correct JSON format, but jsoncpp the string as the correct JSON data, resulting in an error when fetching

Add a validation method that verifies that the data is correct before conversion, before converting

1 BOOLIsjsonillegal (Const Char*jsoncontent)2 {3std::stack<Char>Jsonstr;4     Const Char*p =jsoncontent;5     CharStartchar = jsoncontent[0];6     CharEndChar =' /';7     BOOLIsObject =false;//Prevent {} {} from judging8     BOOLIsArray =false;//prevention of [] judgment9 Ten      while(*p! =' /') One     { AEndChar = *p; -         Switch(*p) -         { the          Case '{': -             if(!IsObject) -             { -IsObject =true; +             } -             Elseif (Jsonstr.empty ())//object repeat into stack +             { A                 return false; at             } -Jsonstr.push ('{'); -              Break; -          Case '"': -             if(Jsonstr.empty () | | jsonstr.top ()! ='"') -             { inJsonstr.push (*p); -             } to             Else +             { - Jsonstr.pop (); the             } *              Break; $          Case '[':Panax Notoginseng             if(!IsArray) -             { theIsArray =true; +             } A             Elseif (Jsonstr.empty ())//array repeat in stack the             { +                 return false; -             } $Jsonstr.push ('['); $              Break; -          Case ']': -             if(Jsonstr.empty () | | jsonstr.top ()! ='[') the             { -                 return false;Wuyi             } the             Else -             { Wu Jsonstr.pop (); -             } About              Break; $          Case '}': -             if(Jsonstr.empty () | | jsonstr.top ()! ='{') -             { -                 return false; A             } +             Else the             { - Jsonstr.pop (); $             } the              Break; the          Case '\\'://escaped characters, skipping thep++; the              Break; -         default: in             ; the         } thep++; About     } the  the     if(Jsonstr.empty ()) the     { +         //Make sure it's an object or an array, not a valid one. -         Switch(Startchar)//Ensure that the symbol corresponds the         {Bayi          Case  '{': the         { the             if(EndChar ='}') -             { -                 return true; the             } the             return false; the         } the          Case '[': -         { the             if(EndChar =']') the             { the                 return true;94             } the             return false; the         } the         default:98             return false; About         } - 101         return true;102     }103     Else104     { the         return false;106     }107}

C + + Jsoncpp parse for JSON string parsing and conversion to complement JSON format validation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.