Creating Documents with the MapR-DB JSON Java API Library

You create and manipulate OJAI documents as Document objects.

Creating a Document by Setting Fields and Values in a New Document Object

Create a new OJAI document by calling the newDocument() method of the MapRDB class, specifying the ID with the setId() method, specifying keys and their values with the set() or setArray() method, and returning the results in a Document object.

For example, suppose that you wanted to create this OJAI document:

{
	"_id" : "movie0000001",
	"title" : "OJAI -- The Documentary",
	"studio" : "MapR Technologies, Inc.",
	"release_date" : "2015-09-29",
	"trailers" : {
		"teaser" : "https://10.10.21.90/trailers/teaser",
		"theatrical" : "https://10.10.21.90/trailers/theatrical"
	},
	"characters" : [
		"Heroic Developer", 
           "Evil Release Manager", 
           "Mad Development Manager"
	],
	"box_office_gross" : 1000000000L
}

You could write a method that looks like this:

  public Document buildDocument() {
    return MapRDB.newDocument()
        .setId("movie0000001")
        .set("title", "OJAI -- The Documentary")
        .set("studio", "MapR Technologies, Inc.")
        .set("release_date", Values.parseDate("2015-09-29"))
        .set("trailers.teaser", "https://10.10.21.90/trailers/teaser")
        .set("trailers.theatrical", "https://10.10.21.90/trailers/theatrical")
        .setArray("characters", ImmutableList.of("Heroic Developer", "Evil Release Manager", "Mad Development Manager"))
        .set("box_office_gross", 1000000000L);
  }

For the full list of methods in the Document interface, see Document.

Creating a Document from a JavaBean

Create a new OJAI document by calling the newDocument(Object bean) method of the MapRDB class and returning the results in a Document object.

For example, suppose that you were using the following JavaBean class:

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"a",
"b",
"array"
})
public class ExampleJson {
    
    @JsonProperty("a")
    private Double a;
    @JsonProperty("b")
    private String b;
    @JsonProperty("array")
    private List<Object> array = new ArrayList<Double>();
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
    /**
     *
     * @return
     * The a
     */
    @JsonProperty("a")
    public Double getA() {
        return a;
    }
    
    /**
     *
     * @param a
     * The a
     */
    @JsonProperty("a")
    public void setA(Double a) {
        this.a = a;
    }
    
    /**
     *
     * @return
     * The b
     */
    @JsonProperty("b")
    public String getB() {
        return b;
    }
    
    /**
     *
     * @param b
     * The b
     */
    @JsonProperty("b")
    public void setB(String b) {
        this.b = b;
    }
    
    /**
     *
     * @return
     * The array
     */
    @JsonProperty("array")
    public List<Object> getArray() {
        return array;
    }
    
    /**
     *
     * @param array
     * The array
     */
    @JsonProperty("array")
    public void setArray(List<Object> array) {
        this.array = array;
    }
    
    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }
    
    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }
    
}

You could create a new bean like this:

ExampleJson bean = new ExampleJson();

bean.setA(1);
bean.setB("aString");

List arrList = new ArrayList();
arrList.add(1);
arrList.add(2);
arrList.add("arrStr");

Map arrMap = new HashMap();
arrMap.put("c","arrMapStr");
arrList.add(arrMap);
bean.setArray(arrList);

After creating a new bean, you can create a document from it like this:

Document pojoDoc = MapRDB.newDocument(bean);

The document will have the following structure:

{  
   "a":1,
   "b":"aString",
   "array":[  
      1,
      2,
      "arrStr",
      {  
         "c":"arrMapStr"
      }
   ]
}

You can also create a JavaBean from an OJAI document. For example, suppose that you modified the document that you created in the example above.

pojoDoc.set("d","10");

You now want to convert the document back into a bean that uses the ExampleJson class shown above. You could do so like this:

ExampleJson bean = pojoDoc.toJavaBean(ExampleJson.class);

Creating a Document from a String

Create a new OJAI document by calling the newDocument(String string) method of the MapRDB class and returning the results in a Document object.

Document pojoDoc = MapRDB.newDocument("{\"a\":1,\"b\":\"aString\",
\"array\":[1,2,\"arrStr\",{\"c\":\"arrMapStr\"}]}");

This code creates the following OJAI document:

{  
   "a":1,
   "b":"aString",
   "array":[  
      1,
      2,
      "arrStr",
      {  
         "c":"arrMapStr"
      }
   ]
}