Git Product home page Git Product logo

simplejson's Introduction

SimpleJSON

Simple C++ JSON library

License

Do what the fuck you want public license

About

SimpleJSON is a lightweight JSON library for exporting data in JSON format from C++. By taking advantage of templates and operator overloading on the backend, you're able to create and work with JSON objects right away, just as you would expect from a language such as JavaScript. SimpleJSON is a single C++ Header file, "json.hpp". Feel free to download this file on its own, and include it in your project. No other requirements!

Platforms

SimpleJSON should work on any platform; it's only requirement is a C++11 compatible compiler, as it make heavy use of the C++11 move semantics, and variadic templates. The tests are tailored for linux, but could be ported to any platform with python support and a C++11 compiler.

API

You can find the API over here. For now it's just a Markdown file with C++ syntax highlighting, but it's better than nothing!

Upcoming Features

SimpleJSON is still missing some features, which I hope to get done soon!

  • Write more test cases to cover all major components( mostly parsing )

One of the biggests goals for SimpleJSON is for it to be lightweight, and small. Having complicated logic isn't bad, but it bloats the codebase in most cases. I'd like to keep things small rather than put in big features that take a ton of space.

If you run into any bugs, or see that I'm missing a featuer, please submit an issue through GitHub and I'll respond as soon as I can!

Example

More examples can be found in the 'examples' directory. Check out the API for a full list of functions.

#include "json.hpp"

int main() {
  json::JSON obj;
  // Create a new Array as a field of an Object.
  obj["array"] = json::Array( true, "Two", 3, 4.0 );
  // Create a new Object as a field of another Object.
  obj["obj"] = json::Object();
  // Assign to one of the inner object's fields
  obj["obj"]["inner"] = "Inside";
  
  // We don't need to specify the type of the JSON object:
  obj["new"]["some"]["deep"]["key"] = "Value";
  obj["array2"].append( false, "three" );
  
  // We can also parse a string into a JSON object:
  obj["parsed"] = JSON::Load( "[ { \"Key\" : \"Value\" }, false ]" );
  
  std::cout << obj << std::endl;
}

Output:

{
  "array" : [true, "Two", 3, 4.000000],
  "array2" : [false, "three"],
  "new" : {
    "some" : {
      "deep" : {
        "key" : "Value"
      }
    }
  },
  "obj" : {
    "inner" : "Inside"
  },
  "parsed" : [{
      "Key" : "Value"
    }, false]
}

This example can also be written another way:

#include "json.hpp"
#include <iostream>

using json::JSON;

int main() {
    JSON obj = {
        "array", json::Array( true, "Two", 3, 4.0 ),
        "obj", {
            "inner", "Inside"
        },
        "new", { 
            "some", { 
                "deep", { 
                    "key", "Value" 
                } 
            } 
        },
        "array2", json::Array( false, "three" )
    };

    std::cout << obj << std::endl;

Sadly, we don't have access to the : character in C++, so we can't use that to seperate key-value pairs, but by using commas, we can achieve a very similar effect. The other point you might notice, is that we have to explictly create arrays. This is a limitation of C++'s operator overloading rules, so we can't use the [] operator to define the array :( I'm looking into ways to make this smoother.

simplejson's People

Contributors

nbsdx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simplejson's Issues

Initializer lists

Why not to use STL-like syntax for initializing pairs of elements?

Like this:
map <int, int> example =
{
{1, 5},
{4, 7}
}

Academic citation

Hi @nbsdx, I'm a postdoctoral researcher at Fermilab (a department of energy laboratory in the Chicago suburbs). I'm working on a journal publication describing a physics simulation tool called MARLEY. A few years ago I found this repo and adapted your SimpleJSON code into a configuration file parser for MARLEY (here's the altered code if you're curious: https://github.com/sjgardiner/marley/blob/master/include/marley/JSON.hh). Thanks for sharing your work, it saved me a lot of pain!

I'd like to cite the SimpleJSON library in my journal article, which includes a lot of material about implementation details. Would you please email me when you get the chance (my address is my last name "@fnal.gov") and let me know what author name I should use in the bibliography? If possible, I'd really like to give you credit for your hard work.

SimpleJSON read out of bounds - information leak

While i was testing SimpleJSON security i found a crash during string parsing inside parse_string function , below a screenshot.

schermata 2017-02-07 alle 01 41 24

This seems to be an information leak bug since the parser will try to parse a string until it found a matching " character in order to close the string inside the object, so providing something similar will result in a read out of bounds!

Step to reproduce:

schermata 2017-02-07 alle 01 45 28

Let me know if you need more information!

Regards,
Daniele Linguaglossa

cannot include json.hpp from more than one cpp file due to multiply defined symbols

including json.hpp in multiple cpp files leads to linker errors:

file1.o: In function `json::Array()':
json.hpp:410: multiple definition of `json::Array()'
file2.o:json.hpp:410: first defined here
file1.o: In function `json::Object()':
json.hpp:421: multiple definition of `json::Object()'

Compiler is gcc 5.1.1 building on Fedora 22

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.