Share your repls and programming experiences

← Back to all posts
JSON in C++
Theboys619 (60)

Hello. So I made a header library for parsing JSON. Im not too experienced with C++ so it's probably not the best. I only started C++ about a month and a half ago.

About The Project

So basically its a JSON Parser in C++.
It can parse a JSON String into an Object that can be used within your project.

Here are some methods with the JSON class.

JSONObject JSON::readFromFile(std::string);
JSONObject JSON::parse(std::string);
std::string JSON::stringify(JSONObject);

You can use subscripts on the object to access different values in the object.

JSONObject x = JSON::readFromFile(std::string);
x["hello"] = "world";

// To get a value use .getTYPE();
std::cout << x["hello"].getString();

There are many methods to get the value. These should be used on the subscript.

std::string getString();
int getInt();
double getDouble();
bool getBoolean();

Right now it can parse I think everything in JSON. It can do arrays, objects, integers, doubles, null, undefined, and booleans.
You can also nest objects and arrays into each other.

For arrays using the subscript is the same as any other array:

// x is defined JSONObject and it is an array
// [2, 4, 5, 6]
std::cout << x[0].getInt() << std::endl; // -> 2

There is no way to get the length of the array yet so...
Also cannot initialize an array easily either. But Im still working on it so maybe in the future.

But thats basically it. It might not be the most performant. I didn't really test performance I just wanted it to work so. Thanks.

EpicGamer007 (1473)

Your better than me, and i started 2 months ago

Theboys619 (60)

Thanks, but hey I know other typed languages so its just a little bit easier to learn it. But you will still be able to learn you got this. It just takes time. @EpicGamer007

RayhanADev (1392)

Awesome! Now it’s moment’s like this when I wish I learned C++.

Theboys619 (60)

Learn it now then you still got time. @RayhanADev

RayhanADev (1392)

@Theboys619 I should... maybe after this project I’m working on!

RohilPatel (1478)

Trust me. Definitely worth it! @RayhanADev

DynamicSquid (4603)

Awesome! Why don't you ignore vscode/ though?

Theboys619 (60)

oh lol I forgot about that folder. @DynamicSquid

fuzzyastrocat (1504)

Ah this is nice. I wish it was for plain C though :)

Theboys619 (60)

Thanks. Although, I don't know much of plain C. Maybe I should try do something in plain C. @fuzzyastrocat

fuzzyastrocat (1504)

@Theboys619 Much of plain C is just reduced C++ and different idioms to replace it, so it probably wouldn't be too difficult to learn.

Theboys619 (60)

Yea thats what I thought. Ill see. @fuzzyastrocat

JBloves27 (1514)

Hmm, idk how C works, but pretty cool!

programmeruser (527)

Great, might consider using this library for JSON parsing or just the JSONObject class.

Theboys619 (60)

Thanks! If you do end up using it and find bugs just @ me on this post and Ill fix it. @programmeruser