Equivalent to JSON.parse () in mongo 3.x driver for Java

JSON.parse() from mongo (the Java driver) returns either BasicDBList or BasicDBObject.

However, when migrating to mongo driver 3.x, what new parsing method returns either Document or List<Document> ?

In the new driver, Document.parse() parses only an object, not an array (throws an exception when specifying an array).

What is equivalent to JSON.parse () for arrays with 3.x Java drivers?

+5
source share
5 answers

Parse JSON string data using mongodb 3.x java driver:

Parse JSON Document:

Use the static Document.parse() method to parse a single JSON document.

 Document doc = Document.parse("{\"objA\":{\"foo\":1}}"); 

Parse JSON array:

Use an instance of BsonArrayCodec to decode JsonReader .

For instance:

 final String JSON_DATA = "[{\"objA\":{\"foo\":1}}," + "{\"objB\":{\"bar\":2}}]"; final CodecRegistry codecRegistry = CodecRegistries.fromProviders(asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider())); JsonReader reader = new JsonReader(JSON_DATA); BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry); BsonArray docArray = arrayReader.decode(reader, DecoderContext.builder().build()); for (BsonValue doc : docArray.getValues()) { System.out.println(doc); } 

ref: http://api.mongodb.org/java/3.2/org/bson/json/JsonReader.html , http://api.mongodb.org/java/3.2/org/bson/codecs/BsonArrayCodec.html

+2
source

How about this:

 Document doc = new Document("array", JSON.parse("[ 100, 500, 300, 200, 400 ]", new JSONCallback())); System.out.println(doc.toJson()); //prints { "array" : [100, 500, 300, 200, 400] } 
0
source

You are right that there is no simple equivalent.

If you use JSON documents with line separators instead of a JSON array, this becomes pretty simple:

 List<Document> getDocumentsFromLineDelimitedJson(final String lineDelimitedJson) { BufferedReader stringReader = new BufferedReader( new StringReader(lineDelimitedJson)); List<Document> documents = new ArrayList<>(); String json; try { while ((json = stringReader.readLine()) != null) { documents.add(Document.parse(json)); } } catch (IOException e) { // ignore, can't happen with a StringReader } return documents; } 

For example, this call

 System.out.println(getDocumentsFromLineDelimitedJson("{a : 1}\n{a : 2}\n{a : 3}")); 

will print:

[Document {{a = 1}}, Document {{a = 2}}, Document {{a = 3}}]

0
source

A simple trick to parse any JSON and get either Document or List<Document> :

 Document.parse("{\"json\":" + json + "}").get("json") 
0
source

The easiest equivalent for me is to use any json library to convert json to POJO. The following is an example of using jackson:

 String input = "[{\"objA\":{\"foo\":1}},{\"objB\":{\"bar\":2}}]"; ObjectMapper mapper = new ObjectMapper(); List<Document> output = (List<Document>) mapper.readValue(input, List.class) .stream().map(listItem -> new Document((LinkedHashMap)listItem)) .collect(Collectors.toList()); 
0
source

Source: https://habr.com/ru/post/1239052/


All Articles