Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Saturday, November 24, 2018

Velocity and rendering JSONObjects or Java Beans

You can use a Velocity template to render either a JsonNode or its corresponding Java bean,
without changing the velocity template.

Code is available https://github.com/vernetto/velocityjson here (the .wm file location is hardcoded with a Widows machine, sorry!)



and the full code is visible here https://github.com/vernetto/velocityjson/blob/master/src/main/java/com/example/demo/VelocityApplication.java

BEWARE the velocity template is stored under the resources folder

The only funny thing is that in the JsonNode the values retain the ""

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]]]






Monday, March 23, 2015

Extending WLST with custom Java classes - the case of JSON parsing

I have recently looked into how to "modernize" the WLST Jithon/Python version - still sadly a prehistoric 2.2.1, while the recent-most Python version is 3.4.2 (Oracle.... how about doing something about WLST? ).

Here is some instructions on how to try this upgrade - but it's not supported, so do it at your own risk.
http://stackoverflow.com/questions/11881659/wlst-vs-jython-weblogic-10-3-3

This is an increasingly painful limitation of WLST, that it can't use any "modern" Python package, like json.

The good news is that it's relatively easy to expand WLST with any Java library you wish: just add the JAR to the CLASSPATH in /opt/oracle/middleware11g/wlserver_10.3/common/bin/wlst.sh, import the Java class with "from com.bla.mypackage import MyClass" and you can use the custom code.

In case of json parsing, there is a popular Google GSON library you could incorporate:

http://search.maven.org/#artifactdetails|com.google.code.gson|gson|2.3.1|jar which comes with a rich User Guide

The API of this GSON is here ... it seems to be aimed at parsing a JSON string directly into a Java object... there doesn't seem to be room for a more casual, random approach.

The Oracle solution is more "casual" http://www.oracle.com/technetwork/articles/java/json-1973242.html and reminiscent of a DOM approach to parsing... more suitable for a quickly hacked deserialization.

Anyway.... I wish I could simply use the Python JSON module...

Monday, October 4, 2010

Data Serialization standards.... beyond XML

I am reading a series of EXCELLENT pages on the Wikipedia:

XML

JSON

YAML


I am not saying one is better than the other. Each has pros and cons.

XML disturbs me for his prolixity, the entities like < and > and &, and the close tags . I like it because it's fairly robust and unambiguous - barred Namespaces.

JSON disturbs me for his quotes. I like its simplicity.

YAML disturbs for the reliance on indentation and special notations. I like the rigorous representation of complex data structures.


But the huge question mark raising in my head is: why on Earth do we need a format that is directly readable/editable by humans?
So that one can use Notepad or vi to edit them?

Ok, then I will give you a YAMLNotepad and a YAMLvi and we move away from a (necessarily inefficient) human readable format and use a more rigorous binary representation, mutuated for instance from Java .ser format or Coherence POF format or TIBCO or whatever other high-speed serialization standard.
We will gain in performance, validation, readibility (you can customize the program to display in whatever format you want, attaching the stylesheets of your choice)...

Why on Earth the actual data on the wire must be in the same format that we display to the user? To me it's only because it's difficult to have a standard be supported across multiple products / platform, and an ASCII editor is the most universal too available. Too bad though.... so much is lost.