Share your repls and programming experiences

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

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.
Example:

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.

Commentshotnewtop
EpicGamer007 (801)

Your better than me, and i started 2 months ago

Theboys619 (58)

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 (1002)

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

Theboys619 (58)

Learn it now then you still got time. @RayhanADev

RayhanADev (1002)

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

RohilPatel (1428)

Trust me. Definitely worth it! @RayhanADev

DynamicSquid (4398)

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

Theboys619 (58)

oh lol I forgot about that folder. @DynamicSquid

fuzzyastrocat (1297)

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

Theboys619 (58)

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

fuzzyastrocat (1297)

@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 (58)

Yea thats what I thought. Ill see. @fuzzyastrocat

JBYT27 (530)

Hmm, idk how C works, but pretty cool!

programmeruser (148)

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

Theboys619 (58)

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