Very simple C++ JSON Parser

Related tags

JSON jute
Overview

jute.svg


Very simple JSON parser for c++



data.json:


{
   "examples": [
      {
         "tag_name": "a",
         "attr": [
            {
               "key": "href",
               "value": "http://amir.saboury.net"
            },
            {
               "key": "target",
               "value": "_blank"
            }
         ]
      },
      {
         "this_is": [
            "array",
            "of",
            "strings"
         ],
         "number_array": [
            1,
            2,
            4,
            8,
            16
         ],
         "pie": 3.14,
         "boolean": true,
         "bug": null,
         "mixed": [
            1,
            2,
            {
               "test1": -1.2345,
               "test2": false
            },
            null,
            0.4,
            [
               "nested",
               [
                  "array",
                  true
               ]
            ],
            "end of story!"
         ]
      },
      {
         "done": true
      }
   ]
}


example "main.cpp" file


#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
#include "jute.h"
using namespace std;

int main () {
    ifstream in("data.json");
    string str = "";
    string tmp;
    while (getline(in, tmp)) str += tmp;
    jute::jValue v = jute::parser::parse(str);
    cout << v.to_string() << endl;
    cout << " ------ " << endl;
    cout << v["examples"][0]["attr"][0]["value"].as_string() << endl;
    if (v["examples"][1]["mixed"][5][1][1].as_bool()) {
        cout << v["examples"][1]["pie"].as_double() << endl;
        cout << v["examples"][2].to_string() << endl;
    }
    // You can get type of a jValue by calling its get_type() function
    // It returns a jType which can be one of these:
    //    {JSTRING, JOBJECT, JARRAY, JBOOLEAN, JNUMBER, JNULL, JUNKNOWN}
    //
    //    if (v["examples"][1]["mixed"][5][1][1].get_type() == jute::JBOOLEAN) ...
    return 0;
}

Note:


This version is not providing error checking. All functions assume the input string is in valid format of JSON. Also number format checking is poor. Improvements are welcome. Read the source code; it is just about 300 LOC 😄

License: MIT

You might also like...
🗄️ single header json parser for C and C++

🗄️ json.h A simple single header solution to parsing JSON in C and C++. JSON is parsed into a read-only, single allocation buffer. The current suppor

a JSON parser and printer library in C. easy to integrate with any model.

libjson - simple and efficient json parser and printer in C Introduction libjson is a simple library without any dependancies to parse and pretty prin

a header-file-only, JSON parser serializer in C++

PicoJSON - a C++ JSON parser / serializer Copyright © 2009-2010 Cybozu Labs, Inc. Copyright © 2011-2015 Kazuho Oku Licensed under 2-clause BSD license

A fast JSON parser/generator for C++ with both SAX/DOM style API
A fast JSON parser/generator for C++ with both SAX/DOM style API

A fast JSON parser/generator for C++ with both SAX/DOM style API Tencent is pleased to support the open source community by making RapidJSON available

Lightweight, extremely high-performance JSON parser for C++11

sajson sajson is an extremely high-performance, in-place, DOM-style JSON parser written in C++. Originally, sajson meant Single Allocation JSON, but i

🔋 In-place lightweight JSON parser

🔋 JSON parser for C This is very simple and very powerful JSON parser. It creates DOM-like data structure and allows to iterate and process JSON obje

RapidJSON is a JSON parser and generator for C++.
RapidJSON is a JSON parser and generator for C++.

A fast JSON parser/generator for C++ with both SAX/DOM style API

single-header json parser for c99 and c++

ghh_json.h a single-header ISO-C99 (and C++ compatible) json loader. why? obviously this isn't the first json library written for C, so why would I wr

Buggy JSON parser

Fuzzgoat: A minimal libFuzzer integration This repository contains a basic C project that includes an (intentionally insecure) JSON parser. It is an e

Comments
  • Clang compilation error

    Clang compilation error

    With Apple LLVM version 7.0.0 (clang-700.0.72),

    jute.cpp:80:22: error: addition of default argument on redeclaration makes this constructor a
          default constructor
    jValue::jValue(jType tp = JUNKNOWN) {
    

    Related discussion

    I think a simple fix is to make another default constructor.

    opened by miloyip 3
  • Whitescape recognition has been changed in order to tolerate tabs too

    Whitescape recognition has been changed in order to tolerate tabs too

    I've replaced the whitespace recognition logic to isspace() in order to tolerate e.g. tab characters as well. I didn't want to eliminate the is_whitespace() intentionally.

    opened by kzaabox 1
  • License

    License

    Hello,

    I found the jute parser very easy to use and fully working for my needs. What is the license of the source? Can it be used in project distributed under BSD-3-Clause license?

    Thank you for answer.

    opened by MrLukass 1
  • Suggestions

    Suggestions

    1. using namespace std in header file will pollute all implementation files that include the header. The practice is to use std::string, std::vector, etc. in header.
    2. Use size_t instead of int for size of array/object and indices. Otherwise there are a lot of related warnings.
    3. Currently, these APIs returns jValue by value.
    jValue operator[](int i);
    jValue operator[](string s);
    

    This will make a deep copy of the whole subtree for every call. If the library is designed for read-only access to the parse result (as I cannot find APIs for modifying existing values), I will suggest to change all APIs as const member functions:

    string to_string() const;
    jType get_type() const;
    int as_int() const;
    double as_double() const;
    bool as_bool() const;
    string as_string() const;
    size_t size() const;
    const jValue& operator[](size_t i) const ;
    const jValue& operator[](const string& s) const;
    

    While the other write-access API can be private and makes parser friend of jValue.

    opened by miloyip 0
Owner
Amir Saboury
life hacker. Shopifolk at @Shopify
Amir Saboury
Very simple C++ JSON Parser

Very simple JSON parser for c++ data.json: { "examples": [ { "tag_name": "a", "attr": [ { "key":

Amir Saboury 67 Nov 20, 2022
Very low footprint JSON parser written in portable ANSI C

Very low footprint JSON parser written in portable ANSI C. BSD licensed with no dependencies (i.e. just drop the C file into your project) Never recur

James McLaughlin 1.2k Nov 27, 2022
Very low footprint JSON parser written in portable ANSI C

Very low footprint JSON parser written in portable C89 (sometimes referred to as ANSI C). BSD licensed with no dependencies (i.e. just drop the C file

null 1.2k Nov 27, 2022
A very sane (header only) C++14 JSON library

JeayeSON - a very sane C++14 JSON library JeayeSON was designed out of frustration that there aren't many template-based approaches to handling JSON i

Jeaye Wilkerson 128 Nov 28, 2022
Very fast Python JSON parsing library

cysimdjson Fast JSON parsing library for Python, 7-12 times faster than standard Python JSON parser. It is Python bindings for the simdjson using Cyth

TeskaLabs 230 Nov 20, 2022
Ultralightweight JSON parser in ANSI C

cJSON Ultralightweight JSON parser in ANSI C. Table of contents License Usage Welcome to cJSON Building Copying the source CMake Makefile Vcpkg Includ

Dave Gamble 8.2k Dec 3, 2022
JSON parser and generator for C/C++ with scanf/printf like interface. Targeting embedded systems.

JSON parser and emitter for C/C++ Features ISO C and ISO C++ compliant portable code Very small footprint No dependencies json_scanf() scans a string

Cesanta Software 633 Nov 27, 2022
JSON & BSON parser/writer

jbson is a library for building & iterating BSON data, and JSON documents in C++14. \tableofcontents Features # {#features} Header only. Boost license

Chris Manning 40 Sep 14, 2022
Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket

JSMN jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You

Serge Zaitsev 3.1k Nov 25, 2022
A JSON parser in C++

JSON++ Introduction JSON++ is a light-weight JSON parser, writer and reader written in C++. JSON++ can also convert JSON documents into lossless XML d

Hong Jiang 496 Dec 1, 2022