Thread: JSON Advise for C++

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    26

    JSON Advise for C++

    I have a string contains JSON:

    Code:
    std::string person = {name:"Melinda", surname:"Rose", address:"New York"};
    Omitted backslashes for easy read.

    I want to have those 3 entries into separate variables. But I am not sure I should code myself or use a library.

    Is it difficult to deal with JSON in C++ or should I use a library like

    https://github.com/open-source-parsers/jsoncpp

    What do you recommend?

    Cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably meant:
    Code:
    std::string person = "{name:\"Melinda\", surname:\"Rose\", address:\"New York\"}";
    Parsing that without a library should not be difficult, but if you are going to have to parse more complex JSON representations, then you might as well use a well-tested library instead of implementing your own parser.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    26
    Quote Originally Posted by laserlight View Post
    You probably meant:
    Code:
    std::string person = "{name:\"Melinda\", surname:\"Rose\", address:\"New York\"}";
    Parsing that without a library should not be difficult, but if you are going to have to parse more complex JSON representations, then you might as well use a well-tested library instead of implementing your own parser.
    Thanks @laserlight for your answer.

    Yes I did it on purpose to show JSON structure clearly.

    It will be simple I suppose. I dont think I will need very much nested JSON strings. Just concerned about some localization like Arabic or Chinese characters or empty spaces.

    Just checked above mentioned library and it compiled 4MB just for lib_json.lib. My original program is 300KB. Am I done something wrong or is this kind of libraries are normally big?

    BTW, could you please guide me how can I parse my JSON string by myself? A code listing would be great.

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JessicaS
    Just checked above mentioned library and it compiled 4MB just for lib_json.lib. My original program is 300KB. Am I done something wrong or is this kind of libraries are normally big?
    Could be a matter of static linking versus dynamic linking, or maybe the library has a large dependency or pulls in more dependencies than necessary.

    I did a quick check, and actually that is invalid JSON. The JSON representation should have been:
    Code:
    {"name":"Melinda", "surname":"Rose", "address":"New York"}
    Quote Originally Posted by JessicaS
    could you please guide me how can I parse my JSON string by myself?
    Well, a simple approach is to do three passes: on each pass, you search for the key ("name", "surname", or "address") such that it is followed by a ":" possibly with whitespace in between. If you fail to find a key followed by a colon, then you know the input is invalid. Otherwise, you then find the first '"' after the colon, upon which you know the value starts. Then you search for another '"' that is not preceded by a backslash, upon which you have obtained the entire string value. If you fail to find such a '"', then the input is invalid. If you do find it, perhaps you could do a bit more validation by checking that the next non-whitespace character is a comma or a closing brace. This approach would allow for plenty of illegal JSON to pass muster, but that might not matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Use Cereal. You will not regret it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by JessicaS View Post
    It will be simple I suppose. I dont think I will need very much nested JSON strings. Just concerned about some localization like Arabic or Chinese characters or empty spaces.
    The easiest way to deal with localization is to use UTF8 encoding in your files. They can be read and manipulated with simple std::strings which exists in the standard library (Cereal can handle UTF8 perfectly fine). If you're going to output it somewhere in Windows (e.g. message box), then make sure to convert it to UTF16LE before (see your other thread).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. advise
    By ftmah1 in forum C Programming
    Replies: 3
    Last Post: 02-05-2015, 01:36 PM
  2. Head First C Google Map associated with JSON file
    By Vesnog in forum C Programming
    Replies: 4
    Last Post: 04-05-2014, 05:57 PM
  3. Need advise
    By krakatao in forum C Programming
    Replies: 36
    Last Post: 03-11-2012, 10:37 PM
  4. please advise....
    By manny in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 06:09 AM
  5. Some Advise..
    By VaSt in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2003, 01:52 PM