简体   繁体   中英

Jackson Write a fieldname to a nested tree

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).

I have implemented the serialization over Jackson 2 as I tested the Json element "rows" will actually never change after all. Here is what I got working for reference:

 /**
 * Generates Json from original Google Analytics report to include extra data on each page.
 * @return output returns the converted Json
 * @throws IOException when the File cannot be read
 */
public void generateJson(File input) throws IOException {

    try {
        //Convert object to JSON string and pretty print.
        ObjectMapper mapper = new ObjectMapper();

        //read the retrieved Json values into the Object members.
        GoogleAnalyticsJsonObject gao = mapper.readValue(input, GoogleAnalyticsJsonObject.class);
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(gao);
        JsonParser parser = mapper.getFactory().createParser(json);
        TokenBuffer buffer = parser.readValueAs(TokenBuffer.class);

        //Regenerate the Json file.
        JsonGenerator gen = mapper.getFactory().createGenerator(
                new File(String.valueOf(input)), JsonEncoding.UTF8);
        gen.useDefaultPrettyPrinter().writeObject(buffer);

        //Close the JsonGenerator.
        gen.close();
        parser.close();
    } catch (JsonGenerationException jge) {
        log.error("JsonGenerationException " + jge);
    }
}

I created a package serialised and put in all the Pojos I generated from jsonschema2pojo and I added a new Object called Title to it. I added the Object like this:

@JsonInclude(JsonInclude.Include.ALWAYS)

@JsonPropertyOrder({ "dimensions", "title", "metrics" }) public class Row {

@JsonProperty("dimensions")
private List<String> dimensions = null;
@JsonProperty("title")
private String title = null;
@JsonProperty("metrics")
private List<Metric> metrics = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

// Set a Sl4J logger
private final Logger log = LoggerFactory.getLogger(Row.class);

/**
 * 
 * @return
 *     The title
 */
@JsonProperty("title")
public String getTitle() {
    String result = "";
    //generate the title from the Page
    //page title for this item
    //get the associated content
    //read out the content fields for : Code, Name, Award, Duration with some conditional logic
    List<String> dimensions = getDimensions();
    String pageTitle = "";
    Title title = new Title();
    for ( String dim : dimensions ) {
        if ( dim.startsWith("www")) {  //filter out the views from the dimensions, leaving us with page URLs
            try {
                result = title.getTitle(dim);
            } catch (FileManagerException e) {
                e.printStackTrace();
            }
        }
    }

    return result;
}

/**
 * 
 * @param title
 *     The title
 */
@JsonProperty("title")
public void setTitle(String title) {
    this.title = title;
}

/**
 *
 * @return
 *     The dimensions
 */
@JsonProperty("dimensions")
public List<String> getDimensions() {
    return dimensions;
}

/**
 *
 * @param dimensions
 *     The dimensions
 */
@JsonProperty("dimensions")
public void setDimensions(List<String> dimensions) {
    this.dimensions = dimensions;
}

/**
 * 
 * @return
 *     The metrics
 */
@JsonProperty("metrics")
public List<Metric> getMetrics() {
    return metrics;
}

/**
 * 
 * @param metrics
 *     The metrics
 */
@JsonProperty("metrics")
public void setMetrics(List<Metric> metrics) {
    this.metrics = metrics;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM