0

I have the following use case -

  1. Store a JSON schema (dynamic, changes over time) in mongodb.
  2. Read JSON objects from a file and validate them against the schema (in #1)
  3. I am using this JSON Validator.
  4. I need to read the schema from mongo db and convert it to JsonNode (Jackson library).

I am using Java..

Can anyone let me know how to convert a mongodb document to JsonNode.. I need this because the validator I am using (mentioned in #3 above) needs a JsonNode to construct the schema object.

EDIT: Is it fine performance wise to convert DBObject to JSON string and then convert it to JsonNode?

abipc
  • 997
  • 2
  • 13
  • 35

2 Answers2

3

Why not go straight from DBObject to JsonNode? iirc, JsonNode is just a map like DBObject is. Converting from one to the other (and back) should be pretty straightforward.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
1

You could use the ObjectReader class (com.fasterxml.jackson.databind.ObjectReader):

ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.reader(JsonNode.class);
JsonNode node = reader.readValue(document.toJson());

Here you can find some performance best practices for Jackson: http://wiki.fasterxml.com/JacksonBestPracticesPerformance

marcosmr
  • 11
  • 2