3

I want to insert pojo object as json document in marklogic using java api. I am using this example as reference which is for inserting pojo as xml document.

I am not able to register my pojo class with the handle for JSON.

public class JSONDocument {
    public static void main(String[] args) throws JAXBException, IOException {
        run(Util.loadProperties());
    }
    @JsonRootName(value = "product")
    static public class Product {
        @JsonProperty
        private String name;
        @JsonProperty
        private String industry;
        @JsonProperty
        private String description;
        public Product() {
            super();
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getIndustry() {
            return industry;
        }
        public void setIndustry(String industry) {
            this.industry = industry;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
    }

    public static void run(ExampleProperties props) throws JAXBException {

        runShortcut(props);

        System.out.println("Wrote, read, and deleted "+Product.class.getName()+" using JAXB");
    }
    public static void runShortcut(ExampleProperties props) throws JAXBException {
        // register the POJO classes like JAXB - JAXBHandle.newFactory(Product.class)

        DatabaseClientFactory.getHandleRegistry().register(
            // Need help here for - registering pojo for JSON

        );
        // create the client
        DatabaseClient client = DatabaseClientFactory.newClient(
                props.host, props.port, props.writerUser, props.writerPassword,
                props.authType);

        // create a manager for JSON documents
        JSONDocumentManager docMgr = client.newJSONDocumentManager();

        // create an instance of the POJO class
        Product product = new Product();
        product.setName("FashionForward");
        product.setIndustry("Retail");
        product.setDescription(
                "(Shortcut) Creates demand with high prices, hours from midnight to dawn, and frequent moves");

        // create an identifier for the document
        String docId = "/example/"+product.getName()+".json";

        // write the POJO as the document content
        docMgr.writeAs(docId, product);

        // ... at some other time ...

        // read the POJO from the document content
        product = docMgr.readAs(docId, Product.class);

        // log the persisted Json document
        System.out.println(docMgr.readAs(docId, String.class));


        // release the client
        client.release();
    }
}

If i am wrong in this example please let me know the right way and help me solve this.

Thanks for reading.

RCS
  • 1,370
  • 11
  • 27

1 Answers1

3

While you could possibly use JAXB to serialize your pojo to JSON, many prefer Jackson and our JacksonDatabindHandle. See an example in JacksonDatabindTest and notice that the City class is registered on lines 68-69.

Or, if you don't need control over what your JSON looks like in the database, the easiest way to persist pojos is with the POJO Data Binding Interface.

Sam Mefford
  • 2,465
  • 10
  • 15
  • Thanks Sam for reply.I will try JacksonDataBindTest. I tried the example of POJO binding with interface. There is one issue with this approach, how do we set the uri of the document while saving the document? – RCS Jun 21 '16 at 17:54
  • Sam,I tried JacksonDataBind example and able to insert the pojo to marklogic database. Need one suggestion in case of updating(add a property) an existing document.There are two way i can think of, either i'll update the pojo and write it back again to db or i'll do partial update like JSON patch example. Which approach will be better and performant, considering i have large number of data to work with. – RCS Jun 22 '16 at 07:22
  • You don't set the uri with PojoRepository, it is set for you based on the classname and the unique id (marked with the @Id annotation). – Sam Mefford Jun 22 '16 at 21:36
  • I think the best way to measure performance is to try it with your data. It's hard to say given what little I know of your application. Just remember that runtime performance isn't the only factor. – Sam Mefford Jun 22 '16 at 21:37
  • I got your point Sam,we can not be judgemental about performance but my question was about going forward with better approach among replacing whole pojo object as josn/add patch to existing json. – RCS Jun 23 '16 at 03:53