Friday, May 18, 2018

JSON to Java, Java to JSON

I am extremely lazy, and I want to model JSON, or Java, but not both at the same time.

In case you want to write JSON first, and then generate Java:

https://github.com/astav/JsonToJava

git clone https://github.com/astav/JsonToJava.git
cd JsonToJava
mvn package
./sample-run.sh
java -cp target/classes/:lib/*:import/ com.astav.jsontojava.JsonToJava sample.json out test TestOutput regex-sample.json false

rules.json
{
  "rules": [
  {"gav":"org.codehaus.plexus:plexus-utils:3.0" , "allow":"false"},
  {"gav":"org.codehaus.plexus:plexus-utils:2.8" , "allow":"true"}
  ]
}


I run this:

export CLASSPATH=target/classes/:lib/*:import/
java com.astav.jsontojava.JsonToJava rules.json out test TestOutput regex-sample.json false

this is what it generates:

RulesEntry.java
package test;

import org.codehaus.jackson.annotate.JsonProperty;
import java.util.*;

public class RulesEntry {
 @JsonProperty("allow") private Boolean allow;
 @JsonProperty("gav") private String gav;
}


TestOutput.java
package test;

import org.codehaus.jackson.annotate.JsonProperty;
import java.util.*;

public class TestOutput {
 @JsonProperty("rules") private List<rulesentry> rules;
}





An alternative is http://www.jsonschema2pojo.org/ , for the same input JSON I get a somehow less accurate modelling:

-----------------------------------com.example.Rule.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
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)
@JsonPropertyOrder({
"gav",
"allow"
})
public class Rule {

@JsonProperty("gav")
private String gav;
@JsonProperty("allow")
private String allow;
@JsonIgnore
private Map additionalProperties = new HashMap();

@JsonProperty("gav")
public String getGav() {
return gav;
}

@JsonProperty("gav")
public void setGav(String gav) {
this.gav = gav;
}

@JsonProperty("allow")
public String getAllow() {
return allow;
}

@JsonProperty("allow")
public void setAllow(String allow) {
this.allow = allow;
}

@JsonAnyGetter
public Map getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Rules.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
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)
@JsonPropertyOrder({
"rules"
})
public class Rules {

@JsonProperty("rules")
private List rules = null;
@JsonIgnore
private Map additionalProperties = new HashMap();

@JsonProperty("rules")
public List getRules() {
return rules;
}

@JsonProperty("rules")
public void setRules(List rules) {
this.rules = rules;
}

@JsonAnyGetter
public Map getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}




To model Java first, then JSON:

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class RulesTest {

 @Test
 public void listToJSON() throws Exception {
  List<Rules> list = new ArrayList<Rules>();
  list.add(new Rule("org.codehaus.plexus:plexus-utils:3.0", true));
  list.add(new Rule("org.codehaus.plexus:plexus-utils:2.6", false));
  Rules rules = new Rules(list);
  final ByteArrayOutputStream out = new ByteArrayOutputStream();
  final ObjectMapper mapper = new ObjectMapper();
  mapper.writeValue(out, rules );

  final byte[] data = out.toByteArray();
  System.out.println(new String(data));
 
  Rules rules2 = mapper.readValue(data, Rules.class);
  System.out.println(rules2);
 }

}

public class Rule {
 String gav;
 Boolean allow;
 public String getGav() {
  return gav;
 }
 public void setGav(String gav) {
  this.gav = gav;
 }
 public Boolean getAllow() {
  return allow;
 }
 public void setAllow(Boolean allow) {
  this.allow = allow;
 }
 @Override
 public String toString() {
  return "Rule [gav=" + gav + ", allow=" + allow + "]";
 }
 public Rule(String gav, Boolean allow) {
  super();
  this.gav = gav;
  this.allow = allow;
 }
 public Rule() {
   super();
 }
 
 
}



import java.util.List;

public class Rules {
 private List<Rule> rules;

 public List<Rule> getRules() {
  return rules;
 }

 public void setRules(List<Rule> rules) {
  this.rules = rules;
 }

 public Rules(List<Rule> rules) {
  super();
  this.rules = rules;
 }
 public Rules() {
  super();
 }
 @Override
 public String toString() {
  return "Rules [rules=" + rules + "]";
 }

}



and I get this

{"rules":[{"gav":"org.codehaus.plexus:plexus-utils:3.0","allow":true},{"gav":"org.codehaus.plexus:plexus-utils:2.6","allow":false}]}
Rules [rules=[Rule [gav=org.codehaus.plexus:plexus-utils:3.0, allow=true], Rule [gav=org.codehaus.plexus:plexus-utils:2.6, allow=false]]]






No comments: