Jsoncpp Use Experience

Source: Internet
Author: User

Jsoncpp the method to determine whether a field exists is as follows:


1) if (root["url"].type ()! = Json::nullvalue)

2) if (value["Sex"].isnull ())



Precautions

1) jsoncpp is a JSON library used by C + + to implement the syntax of JSON by overloading the brackets [], but because C + +

is a strongly typed language, jsoncpp in the implementation process, using a large number of assertions, if the type is encountered incorrectly

, the assertion is enforced, causing the program to exit

For example, pass the following string to the Jsoncpp

std::string strcontext = "Success": false, "MSG": "Gallium sentence switches arrival version ā æ 縞 ode:huoqushebeitongdaoshijianchuo11"

Json::reader read ();

Json::value Root;

if (NULL = = Read.parse (strcontext, Root)) return-1;

Although the string is not a proper JSON-formatted string, it will be parsed as well, resulting in a program that crashes, exits, and goes into the assertion, regardless of the field you use.

The solution is to specify strict JSON message parsing rules

std::string strcontext = "Success": false, "MSG": "Gallium sentence switches arrival version ā æ 縞 ode:huoqushebeitongdaoshijianchuo11"

Json::reader Read (Json::features::strictmode ());

Json::value Root;

if (NULL = = Read.parse (strcontext, Root)) return-1;

There won't be any anomalies like this.


Because Jsoncpp parses an illegal JSON, it is automatically fault-tolerant into character types. When a label is removed for a character type, the assert termination process is triggered.


Reference

Https://www.2cto.com/kf/201409/339350.html

http://blog.csdn.net/sxyizhiren/article/details/18625193



2) Json::value inside the function only Asint, asuint, and does not go to 64-bit integer function, so in the case of returning 64-bit integer, you can first convert to a string, using asstring, and then convert the string to 64-bit integer, indifferent night provides

One option is to convert to floating point, asdouble


Reference

Http://www.mamicode.com/info-detail-78967.html


Jsoncpp Use Experience

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.