I am writing C++ using mingw-w64 g++ on windows with VS Code.
I need to be able to parse some JSON string.
mingw doesn't seem to have any built-in JSON support.
What is the way to set up JSON support in mingw-w64 on windows 10?
There are quite a few libraries for handling JSON from C/C++ that you can use.
To name a few that I have been able to compile with MinGW-w64:
JSON-C
Description : JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects.
https://github.com/json-c/json-c
libjansson
Description : Jansson is a C library for encoding, decoding and manipulating
JSON data.
http://www.digip.org/jansson/
libjson-glib
Description : JSON-GLib is a library providing serialization and deserialization support for the JavaScript Object Notation (JSON) format described by RFC 4627.
Website URL : http://live.gnome.org/JsonGlib
json-parser
Description : Very low footprint JSON parser written in portable ANSI C
https://github.com/udp/json-parser
jsonh
Description : json parser for C and C++
https://github.com/sheredom/json.h
jsmn
Description : jsmn (pronounced like "jasmine") is a minimalistic JSON parser
in C. It can be easily integrated into the resource-limited projects or embedded systems.
http://zserge.com/jsmn.html
tiny-json
Description : tiny-json is a versatile and easy to use json parser in C suitable for embedded systems. It is fast, robust and portable. It is not only a tokenizer. You can get data in string format or get the primitives values in C type
variables without performance loss.
https://github.com/rafagafe/tiny-json
ujson4c
Description : A more user friendly layer for decoding JSON in C/C++ based on
the ultra fast UltraJSON library
https://github.com/esnme/ujson4c/
cajun-jsonapi
Description : CAJUN is a C++ API for the JSON data interchange format with an emphasis on an intuitive, concise interface. The library provides JSON types and operations that mimic standard C++ as closely as possible in concept and design.
https://github.com/cajun-jsonapi/cajun-jsonapi
frozen
Description : JSON parser and generator for C/C++ with scanf/printf like interface. Targeting embedded systems.
Website URL : https://github.com/cesanta/frozen
jq
Description : jq is a lightweight and flexible command-line JSON processor.
https://stedolan.github.io/jq/
js0n
Description : Flexible Zero-Footprint JSON Parser in C
https://github.com/quartzjer/js0n
libfastjson
Description : a fast json library for C
https://github.com/rsyslog/libfastjson
libxo
Description : The libxo library allows an application to generate text, XML,
JSON, and HTML output using a common set of function calls. The application decides at run time which output style should be produced.
https://github.com/Juniper/libxo
microjson
Description : Tiny JSON parser in C that uses only fixed-extent storage.
http://www.catb.org/esr/microjson/
minijsonreader
Description : A DOM-less JSON parser that can parse a JSON object without allocating a single byte of memory
https://github.com/giacomodrago/minijson_reader
minijsonwriter
Description : A simple, little-overhead, allocation-free, and extensible C++
JSON writer, directly wrapping a std::ostream
https://github.com/giacomodrago/minijson_writer
pdjson
Description : A public domain JSON parser focused on correctness, ANSI C99 compliance, full Unicode (UTF-8) support, minimal memory footprint, and a simple API. As a streaming API, arbitrary large JSON could be processed with a small amount of memory (the size of the largest string in the JSON). It seems most C JSON libraries suck in some significant way: broken string support (what if the string contains \u0000?), broken/missing Unicode support, or crappy software license (GPL or "do no evil"). This library intends to avoid these flaws.
https://github.com/skeeto/pdjson
picojson
Description : a header-file-only, JSON parser serializer in C++
https://github.com/kazuho/picojson
sajson
Description : Lightweight, extremely high-performance JSON parser for C++11
https://github.com/chadaustin/sajson
smalljsonparser
Description : This is a simple, one-file JSON parser in C. It is designed for highly resource-constrained systems. It uses no memory allocation, and can stream data, so that the whole file does not need to reside in memory.
https://github.com/DagAgren/SmallJSONParser
univalue
Description : C++ universal value object and JSON library
https://github.com/jgarzik/univalue
Following Brecht's list, I tried json-parser. The following is how I made it to work. Hope this will help folks not familiar with the process
Do this from Msys terminal which came with MinGw G++, becaue it has 'make' command.
cd mycppbase
git clone https://github.com/json-parser/json-parser.git
cd json-parser
export PATH=/c/msys64/mingw64/bin:$PATH
./configure
make
three files are important
json.h
libjsonparser.a
libjsonparser.so
cd myexampledir/
g++ myjson.cpp -o myjson \
-I "/c/.../mycppbase/json-parser" \
-L "/c/.../mycppbase/json-parser" \
-l:libjsonparser.a
UPDATE: 2022/11/20
the previous example is to link a static executable.
To link dynamically, we need to rename the .so file to .dll file. (see comments below)
The following is done in gitbash terminal and worked.
mv libjsonparser.so libjsonparser.dll
cd myexampledir/
g++ myjson.cpp -o myjson \
-I "/c/.../mycppbase/json-parser" \
-L "/c/.../mycppbase/json-parser" \
-ljsonparser
Related
I've got a project that gets metadata from Minecraft mods, and I'm having some trouble with the Minecraft Forge's old mcmod.info format - which is a JSON format read with GSON for those that don't know.
Specifically, GSON unfortunately allows for strings to be multi-line (it allows for unescaped newlines in a string) - which Go's encoding/json doesn't allow for. See the below example from the Chisel mod to see what I mean.
[{
"credits": "AUTOMATIC_MAIDEN for the original mod,
asie for porting to 1.7.2,
and Pokenfenn/Cricket for continuing it in 1.7.
This mod uses textures from the Painterly Pack: http://painterlypack.net/."
}]
This results in an error of invalid character '\n' in string literal.
I did take a brief look at using an alternative JSON parser (the aptly-named jsonparser specifically took my eye), but without testing them all - I've been unable to determine which, if any, support what I need.
I suspect the solution to this problem will be in using an alternative JSON parser, I'm just not aware enough of the available libraries or JSON's use in Golang to make a highly informed decision.
I have a following use case :
parse the json from stream (kafka topic)
extract some fields (likely 35 out of 100 fields)
Build a json out of those fields
Publish it to pub/sub for further processing
My implementation is very much java language bound. Can anyone suggest optimal solution for this ? and why is it optimal ?
For json parsing, I am thinking of https://bolerio.github.io/mjson/
Kafka includes Jackson JSON library and includes its own JSON Deserializer that returns a JsonNode class
Alternatively,as listed in the comments, you can use higher level frameworks such as Spring, Vertx, Quarkus, etc to build Kafka consumers
For the listed use case, I would opt for Spark, Flink, or NiFi for integration with PubSub. Each also offering JSON proessing, with NiFi being more advanced in that it can do JSONPath
I want to parse JSON data from a RESTful service.
Unlike a SOAP-based service, where a service consumer can create stubs and skeleton from WSDL, in the case of the RESTful service, the service consumer gets a raw JSON string.
Since the service consumer does not have a Java object matching the JSON structure, we are not able to use the JSON to Java Mappers like GSON, Jackson etc.
One another way is to use parsers like JsonPath, minimal-json, etc which help traversing the JSON structure and read the data.
Is there any better way of reading JSON data?
The official docs for Jackson mention 3 different ways to parse a JSON doc from Java. The first 2 do not require "Java object matching the JSON structure". In Summary :
Streaming API (aka "Incremental parsing/generation") reads and writes JSON content as discrete events.
Tree Model provides a mutable in-memory tree representation of a JSON document. ObjectMapper can build trees that consist of JsonNode nodes.
Data Binding converts JSON to and from POJOs based either on property accessor conventions or annotations.
With simple data binding you convert to and from Java Maps, Lists, Strings, Numbers, Booleans and nulls
With full data binding you convert to and from any Java bean type (as well as "simple" types mentioned above)
Another option is to generate Java Beans from JSON documents. You mileage may vary and you may/probably will have to modify the generated files. There are at least 5 online tools for that purpose that you can try:
http://www.jsonschema2pojo.org/
http://pojo.sodhanalibrary.com/
https://timboudreau.com/blog/json/read
http://jsongen.byingtondesign.com/
http://json2java.azurewebsites.net/
There are also IDE plugins that you can use. For instance this one for Intellij https://plugins.jetbrains.com/idea/plugin/7678-jackson-generator-plugin
The GSON supports work without objects, too. Something as this:
JsonObject propertiesWrapper = new JsonParser().parse(responseContent).getAsJsonObject();
assertNotNull(propertiesWrapper);
propertiesWrapper = propertiesWrapper.getAsJsonObject("properties");
assertNotNull(propertiesWrapper);
JsonArray propertiesArray = propertiesWrapper.getAsJsonArray("property");
assertNotNull(propertiesArray);
assertTrue(propertiesArray.size()>0, "The list of properties should not be empty. ");
The problem is that the work this way is so inconvenient that it is really better to create objects instead.
Jackson has absolutely the same problems, and to greater extent - extremal inconvenient for direct json reading/creation. All its tutorials advice to use POJOs instead, too.
The only really convenient way is use Groovy. Groovy works as an envelope on Java, you can simply write Java code and use Groovy operators at need. And in JSON or XML reading and creation Groovy is incomparably more powerful that Java with all its libraries multiplied on each other! It is even much more convenient than already prepared by somebody else tree structure of ready POJOs.
In my Delphi 10.1 Berlin Datasnap REST application, I need to customize the JSON serializaton of an object.
I would like to find a solution that makes use of the JSONReflect attribute, and doesn't involve the creation of Converters and Reverters for every specific field, as described in this article by Daniele Teti.
In particular, I'm trying to serialize an object that contains:
a binary file, to convert in JSON representation - like a byte array
some TDateTime fields, to convert in a String with ISO format
I have found a technical PDF document by Marco Cantù, that talks about JSONReflect attribute to enable conversion of fields, but I cannot find documentation about it.
Anyone can help me, please?
Use of JSONReflect attribute automatically implies use of converters and reverters. Delphi XE6 ships with sample project MarshallUnmarshall where JSON serialization is covered. RAD Studio Demo Code is also available online.
If you want to serialize an object that contains a TDateTime field in a string with ISO format, you can also use standard Tjson class defined in Rest.Json unit. It contains an ObjectToJsonString method. In the AOptions parameter you can specify to format dates using ISO standard.
class function ObjectToJsonString(AObject: TObject; AOptions: TJsonOptions = [joDateIsUTC, joDateFormatISO8601]): string;
To serialize a binary file in JSON Daniele Teti has sample code in his Delphi Cookbook. Unfortunately I can not share sample code I think. Recommended reading! Second edition has just appeared.
suppose I have this list in R
x = list(a=1:3,b=8:20)
and I write this to a json file on disk with
library(jsonlite)
cat(toJSON(x),file="f.json")
how can I use the Julia JSON package to read that? Can I?
# Julia
using JSON
JSON.parse("/Users/florianoswald/f.json")
gives a mistake - I guess it expects a json string.
Any alternatives? I would benefit from being able to pass a list (i.e. a nested structure) rather than tabular data. thanks!
If you want to do this with the current version of JSON you can use Julia's readall method to get a string from a file.
Pkg.clone("JSON") will get you the latest development version of JSON.jl (as opposed to the latest released version) – it seems parsefile is not released yet.