Brian Kates Brian Kates - 6 months ago 52
JSON Question

Java structure for Datatables editor json

I need a Java data structure for some JSON data passed to me by the Datatables Editor. The format of the data received is this:

{
"action":"edit",
"data": {
"1009558":{
"weekNumber":"2"
... (more properties)
}
}
}


Here's the full documentation: https://editor.datatables.net/manual/server

Edit: The documentation shows the data sent as form params. I am stringifying the data and sending it as JSON. An example is above.

"1009558" is the row ID. If there are multiple rows sent by the editor, there would be multiple array entries (each with an ID).

Can anyone offer some advice on how to make a Java data structure for deserialization (by Spring MVC)? I can map "action" easy enough, but I'm getting stuck on the "data" element.

Answer

I'd rather suggest you to use jackson.

Here's an example, that you're asking for:

package com.github.xsavikx.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class JacksonTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, DatabaseRow> data = new HashMap<>();
        DatabaseRow row = new DatabaseRow(2, "someData");
        data.put("1009558", row);
        String action = "action";
        DatabaseEntry dbEntry = new DatabaseEntry();
        dbEntry.setAction(action);
        dbEntry.setData(data);
        System.out.println(objectMapper.writeValueAsString(dbEntry));
    }
}

And the result:

{"action":"action","data":{"1009558":{"weekNumber":2,"someData":"someData"}}}

Models:

package com.github.xsavikx.jackson;

import java.util.Map;

public class DatabaseEntry {
    private String action;
    private Map<String, DatabaseRow> data;

    public DatabaseEntry() {

    }

    public DatabaseEntry(String action, Map<String, DatabaseRow> data) {
        this.action = action;
        this.data = data;
    }

    public Map<String, DatabaseRow> getData() {

        return data;
    }

    public void setData(Map<String, DatabaseRow> data) {
        this.data = data;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}

package com.github.xsavikx.jackson;

public class DatabaseRow {
    private int weekNumber;
    private String someData;
    public DatabaseRow(){
    }
    public DatabaseRow(int weekNumber, String someData) {
        this.weekNumber = weekNumber;
        this.someData = someData;
    }

    public int getWeekNumber() {
        return weekNumber;
    }

    public void setWeekNumber(int weekNumber) {
        this.weekNumber = weekNumber;
    }

    public String getSomeData() {
        return someData;
    }

    public void setSomeData(String someData) {
        this.someData = someData;
    }
}

Update: more generic solution with Map of maps:

package com.github.xsavikx.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JacksonTest {
    public static void main(String[] args) throws IOException {
        serializeTest();
        deserializeTest();
    }
    private static void deserializeTest() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        DatabaseEntry databaseEntry = objectMapper.readValue("{\"action\":\"action\",\"data\":{\"1009558\":{\"weekNumber\":2,\"someData\":\"someData\"}}}", DatabaseEntry.class);
        System.out.println(databaseEntry);
    }

    private static void serializeTest() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,Map<String,String>> data = new HashMap<>();
        Map<String,String> values = new HashMap<>();
        values.put("weekDay","2");
        values.put("unpredictableValue","value");
        data.put("1009558", values);
        String action = "action";
        DatabaseEntry dbEntry = new DatabaseEntry();
        dbEntry.setAction(action);
        dbEntry.setData(data);
        System.out.println(objectMapper.writeValueAsString(dbEntry));
    }
}

Model: package com.github.xsavikx.jackson;

import java.util.Map;

public class DatabaseEntry {
    private String action;
    private Map<String, Map<String,String>> data;

    public DatabaseEntry() {

    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public Map<String, Map<String, String>> getData() {
        return data;
    }

    public void setData(Map<String, Map<String, String>> data) {
        this.data = data;
    }
}
Comments