All Projects → beached → json_to_cpp

beached / json_to_cpp

Licence: BSL-1.0 license
Generate C++ class from JSON data

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to json to cpp

Cachingframework.redis
Distributed caching based on StackExchange.Redis and Redis. Includes support for tagging and is cluster-compatible.
Stars: ✭ 209 (+397.62%)
Mutual labels:  serialization
Qs
Quick serialization of R objects
Stars: ✭ 225 (+435.71%)
Mutual labels:  serialization
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: ✭ 242 (+476.19%)
Mutual labels:  serialization
Krate
A SharedPreferences wrapper powered by Kotlin delegates
Stars: ✭ 213 (+407.14%)
Mutual labels:  serialization
Api struct
API wrapper builder with response serialization
Stars: ✭ 224 (+433.33%)
Mutual labels:  serialization
Django Data Wizard
🧙⚙️ Import structured data (e.g. Excel, CSV, XML, JSON) into one or more Django models via an interactive web-based wizard
Stars: ✭ 227 (+440.48%)
Mutual labels:  serialization
Dart Json Mapper
Serialize / Deserialize Dart Objects to / from JSON
Stars: ✭ 206 (+390.48%)
Mutual labels:  serialization
Unicorn
A Sitecore utility designed to simplify deployment of Sitecore items across environments automatically
Stars: ✭ 252 (+500%)
Mutual labels:  serialization
Hyperion
Polymorphic serialization for .NET
Stars: ✭ 225 (+435.71%)
Mutual labels:  serialization
Thorsserializer
C++ Serialization library for JSON
Stars: ✭ 241 (+473.81%)
Mutual labels:  serialization
Libcbor
CBOR protocol implementation for C
Stars: ✭ 215 (+411.9%)
Mutual labels:  serialization
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: ✭ 219 (+421.43%)
Mutual labels:  serialization
Hprose Html5
Hprose is a cross-language RPC. This project is Hprose 2.0 Client for HTML5
Stars: ✭ 237 (+464.29%)
Mutual labels:  serialization
Mashumaro
Fast and well tested serialization framework on top of dataclasses
Stars: ✭ 208 (+395.24%)
Mutual labels:  serialization
Depot.js
📦 depot.js is a storage library with a simple API
Stars: ✭ 247 (+488.1%)
Mutual labels:  serialization
Home
A configurable and eXtensible Xml serializer for .NET.
Stars: ✭ 208 (+395.24%)
Mutual labels:  serialization
Prefser
Wrapper for Android SharedPreferences with object serialization and RxJava Observables
Stars: ✭ 228 (+442.86%)
Mutual labels:  serialization
Cereal
A C++11 library for serialization
Stars: ✭ 2,986 (+7009.52%)
Mutual labels:  serialization
Cbor
CBOR RFC 7049 (Go/Golang) - safe & fast with standard API + toarray & keyasint, CBOR tags, float64/32/16, fuzz tested.
Stars: ✭ 250 (+495.24%)
Mutual labels:  serialization
Parse5
HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
Stars: ✭ 2,778 (+6514.29%)
Mutual labels:  serialization

JSON to C++

This program will take either a json file or a URL to a web service and build C++ classes to work with that data. By default, it will create the serialization linkage for the JsonLink library that is part of https://github.com/beached/daw_json_link .

  • std::optional is used for optional members(those that are not always there or are null in some cases
  • std::string is used for strings
  • int64_t is used for integral types
  • double is used for real types
  • bool is used for boolean types
  • classes are given the name of their member suffixed with a "_t"
  • identifier names are filtered such that C++ keywords, empty id's, or all number id's are prefixed with _json
  • Any character in an id that isn't A-Za-z0-9 or _ will be escaped via 0xXXX
  • Autogenerated types are their member name suffixed with a _t.

Running

To output the C++ code to the terminal one just needs to type json_to_cpp_bin --in_file jsonfile.json or for a url something like json_to_cpp_bin --in_file http://ip.jsontest.com/

Command line options
Options:
  --help                                print option descriptions
  --in_file arg                         json source file path or url
  --kv_paths arg                        Specify class members that are key 
                                        value pairs
  --use_jsonlink arg (=1)               Use JsonLink serializaion/deserializati
                                        on
  --has_cpp20 arg (=0)                  Enables use of non-type class template 
                                        arguments
  --output_file arg                     output goes to c++ header file.
  --allow_overwrite arg (=0)            Overwrite existing output files
  --hide_null_only arg (=1)             Do not output json entries that are 
                                        only ever null
  --use_string_view arg (=0)            Use std::string_view instead of 
                                        std::string.  Must ensure buffer is 
                                        available after parsing when this is 
                                        used
  --root_object arg (=root_object)      Name of the nameless root object
  --user_agent arg (=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36)
                                        User agent to use when downloading via 
                                        URL

Example

H2 JSON Data

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },  
                    "GlossSee": "markup"
                }   
            }   
        }   
    }   
}

Generated C++ Code by calling json_to_cpp --in_file name.json

#include <daw/json/daw_json_link.h>
#include <string>
#include <tuple>
#include <vector>

struct GlossDef_t {
  std::string para;
  std::vector<std::string> GlossSeeAlso;
}; // GlossDef_t

struct GlossEntry_t {
  std::string ID;
  std::string SortAs;
  std::string GlossTerm;
  std::string Acronym;
  std::string Abbrev;
  GlossDef_t GlossDef;
  std::string GlossSee;
}; // GlossEntry_t

struct GlossList_t {
  GlossEntry_t GlossEntry;
}; // GlossList_t

struct GlossDiv_t {
  std::string title;
  GlossList_t GlossList;
}; // GlossDiv_t

struct glossary_t {
  std::string title;
  GlossDiv_t GlossDiv;
}; // glossary_t

struct root_object_t {
  glossary_t glossary;
}; // root_object_t

namespace daw::json {
  template<>
  struct json_data_contract<GlossDef_t> {
    static constexpr char const mem_para[] = "para";
    static constexpr char const mem_GlossSeeAlso[] = "GlossSeeAlso";
    using type = json_member_list<
      json_string<mem_para>,
      json_array<mem_GlossSeeAlso, std::string, std::vector<std::string>>>;

    static inline auto to_json_data( GlossDef_t const &value ) {
      return std::forward_as_tuple( value.para, value.GlossSeeAlso );
    }
  };
} 
namespace daw::json {
  template<>
  struct json_data_contract<GlossEntry_t> {
    static constexpr char const mem_ID[] = "ID";
    static constexpr char const mem_SortAs[] = "SortAs";
    static constexpr char const mem_GlossTerm[] = "GlossTerm";
    static constexpr char const mem_Acronym[] = "Acronym";
    static constexpr char const mem_Abbrev[] = "Abbrev";
    static constexpr char const mem_GlossDef[] = "GlossDef";
    static constexpr char const mem_GlossSee[] = "GlossSee";
    using type = json_member_list<
      json_string<mem_ID>, json_string<mem_SortAs>, json_string<mem_GlossTerm>,
      json_string<mem_Acronym>, json_string<mem_Abbrev>,
      json_class<mem_GlossDef, GlossDef_t>, json_string<mem_GlossSee>>;

    static inline auto to_json_data( GlossEntry_t const &value ) {
      return std::forward_as_tuple( value.ID, value.SortAs, value.GlossTerm,
                                    value.Acronym, value.Abbrev, value.GlossDef,
                                    value.GlossSee );
    }
  };
} 
namespace daw::json {
  template<>
  struct json_data_contract<GlossList_t> {
    static constexpr char const mem_GlossEntry[] = "GlossEntry";
    using type = json_member_list<json_class<mem_GlossEntry, GlossEntry_t>>;

    static inline auto to_json_data( GlossList_t const &value ) {
      return std::forward_as_tuple( value.GlossEntry );
    }
  };
} 
namespace daw::json {
  template<>
  struct json_data_contract<GlossDiv_t> {
    static constexpr char const mem_title[] = "title";
    static constexpr char const mem_GlossList[] = "GlossList";
    using type = json_member_list<json_string<mem_title>,
                                  json_class<mem_GlossList, GlossList_t>>;

    static inline auto to_json_data( GlossDiv_t const &value ) {
      return std::forward_as_tuple( value.title, value.GlossList );
    }
  };
} 
namespace daw::json {
  template<>
  struct json_data_contract<glossary_t> {
    static constexpr char const mem_title[] = "title";
    static constexpr char const mem_GlossDiv[] = "GlossDiv";
    using type = json_member_list<json_string<mem_title>,
                                  json_class<mem_GlossDiv, GlossDiv_t>>;

    static inline auto to_json_data( glossary_t const &value ) {
      return std::forward_as_tuple( value.title, value.GlossDiv );
    }
  };
} 
namespace daw::json {
  template<>
  struct json_data_contract<root_object_t> {
    static constexpr char const mem_glossary[] = "glossary";
    using type = json_member_list<json_class<mem_glossary, glossary_t>>;

    static inline auto to_json_data( root_object_t const &value ) {
      return std::forward_as_tuple( value.glossary );
    }
  };
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].