I have the following use case:
I have to write "title": "G103 MMath Mathematics (4 years)" into a Json String which forms part of a larger Json tree:
{
"dimensions": [
"www.southampton.ac.uk/maths/undergraduate/courses/g100_mathematics.page",
"\"People Also Viewed\""
],
"metrics": [
{
"values": [
"1275"
]
}
]
},
The Json has to become like this:
{
"dimensions": [
"www.southampton.ac.uk/maths/undergraduate/courses/g103_mmath.page",
"\"People Also Viewed\""
],
"title": "G103 MMath Mathematics (4 years)",
"metrics": [
{
"values": [
"105"
]
}
]
}
I am using Java 6 for now (will become Java 8 in the near future). I have reviewed Gson and Jackson and understand that there is also Boon. I have tried to do this in Gson and could not figure out how to read the Json String without creating Pojos (using http://www.jsonschema2pojo.org/). So I decided to use Jackson 2.5.1. I got to the point that I can create a TokenBuffer with the Json pretty printed in there, and I can write the tree read from the Json String to a new file.
Here is the method:
/**
* Generates Json from original Google Analytics report to include extra data on each page.
* @param json input Json String
* @return output returns the converted Json
* @throws IOException when the File cannot be read
*/
public String generateJson(String json) throws IOException {
String output;
//Create a JsonGenerator
ObjectMapper mapper = new ObjectMapper();
JsonParser parser = mapper.getFactory().createParser(json);
TokenBuffer buffer = parser.readValueAs(TokenBuffer.class);
JsonGenerator gen = mapper
.getFactory()
.createGenerator(new File("/Users/arnout/dev/southampton/sitepublisher.git/soton-test/workarea/resultJson.json"), JsonEncoding.UTF8);
gen
.useDefaultPrettyPrinter()
.writeObject(buffer);
// read json in buffer back as tree
JsonNode root = mapper.readTree(buffer.asParser());
JsonNode dimensions = null;
log.debug("GoogleAnalyticsGenerator --- generateJson -- Jackson generated json in TokenBuffer is " + root );
int count = root.get("reports").get(0).get("data").get("rows").size();
for ( int i = 0; i < count ; i++ ){
dimensions = root.get("reports").get(0).get("data").get("rows").get(i).get("dimensions");
log.debug("GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is " + dimensions );
}
gen.close();
parser.close();
//Close the JsonGenerator
output = json.toString();
return output;
}
}
Currently, when I run my unit test on this method I get the Json buffer returned and I can get the JsonNode dimensions back (with results from Google Analytics).
5165 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson generated json in TokenBuffer is {"reports":[{"columnHeader":{"dimensions": ["ga:pagePath","ga:segment"],"metricHeader":{"metricHeaderEntries":[{"name":"pageviews","type":"INTEGER"}]}},"data":{"maximums":[{"values":["1356"]}],"minimums":[{"values":["2"]}],"rowCount":150,"rows":[{"dimensions":["www.southampton.ac.uk/maths/undergraduate/courses/g100_mathematics.page","\"People Also Viewed\""],"metrics":[{"values":["1356"]}]},{"dimensions":["www.southampton.ac.uk/maths/undergraduate/courses/g103_mmath.page","\"People Also Viewed\""],"metrics":[{"values":["105"]}]},{"dimensions":["www.southampton.ac.uk/maths/undergraduate/courses/g120_mathematical_studies.page","\"People Also Viewed\""],"metrics":[{"values":["103"]}]},{"dimensions":["www.southampton.ac.uk/maths/undergraduate/courses/g1nh_maths_with_finance.page","\"People Also Viewed\""],"metrics":[{"values":["73"]}]},{"dimensions":["www.southampton.ac.uk/maths/undergraduate/courses/g1n3_maths_with_actuarial_science.page","\"People Also Viewed\""],"metrics":[{"values":["69"]}]},{"dimensions":["www.southampton.ac.uk/maths/undergraduate/courses/g1g3_maths_with_statistics.page","\"People Also Viewed\""],"metrics":[{"values":["50"]}]}],"samplesReadCounts":["488083"],"samplingSpaceSizes":["867358"],"totals":[{"values":["2557"]}]},"nextPageToken":"6"}]}
5165 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is ["www.southampton.ac.uk/maths/undergraduate/courses/g100_mathematics.page","\"People Also Viewed\""]
5165 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is ["www.southampton.ac.uk/maths/undergraduate/courses/g103_mmath.page","\"People Also Viewed\""]
5165 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is ["www.southampton.ac.uk/maths/undergraduate/courses/g120_mathematical_studies.page","\"People Also Viewed\""]
5165 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is ["www.southampton.ac.uk/maths/undergraduate/courses/g1nh_maths_with_finance.page","\"People Also Viewed\""]
5166 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is ["www.southampton.ac.uk/maths/undergraduate/courses/g1n3_maths_with_actuarial_science.page","\"People Also Viewed\""]
5166 [main] DEBUG u.a.s.l.g.a.GoogleAnalyticsGenerator - GoogleAnalyticsGenerator --- generateJson -- Jackson root dimension array is ["www.southampton.ac.uk/maths/undergraduate/courses/g1g3_maths_with_statistics.page","\"People Also Viewed\""]
The question I have is how do I go about adding the title?
I am happy to use Gson or Boon if that is faster/easier (or any other platform for that matter).