4

I am new to Jackson and I am having some problems determining the best way to deal with processing JSON files that are dynamic in nature. I know I could solve the issue with the streaming or tree API, but this would involve a lot of code which will not be easily maintained. For example, take the following two json files:

{
   something: "somethingValue"
   somethingelse: "anotherValue"
   url: "http://something.com"
}

and

{
   something: "somethingValue"
   somethingelse: "anotherValue"
   url: {
           service1: [
              "http://something.com",
              "https://something.com" ],
           service2: [
              "http://something2.com",
              "https://something2.com" ],
        }
}

the default behaviour of the first json object after being parsed, should add the URL to both service1 and service2 url lists in the subclass "URL". where the second allow specifying very specific urls to each. The data object for url class I was planning on use is as follows:

public class url {

   // ideally, I would use the java.net.URL instead of String
   public List<String> service1;    
   public List<String> service2;

   // also includes getter/setters using a fluent style
   ...
}

There would also be some other parent class which would have a parameter for URL and other first level json parameters.

What is the best way to handle this in jackson?

3 Answers 3

1

The second one is not valid JSON, this is :

{
   "something": "somethingValue",
   "somethingelse": "anotherValue",
   "url": {
           "service1" : [
              "http://something.com",
              "https://something.com" ],
           "service2" : [
              "http://something2.com",
              "https://something2.com" ]
        }
}

You can create it/consume it with class A which looks like following

class A{
 String something;
 String somethingElse;
 B url;
}

class B{
 Str service1;
 List<String> service2;
}

To achieve anything dynamically no matter what, you have to put it in Lists, therefore instead of solution above, you can do this

class A{
 String something;
 String somethingElse;
 B url;
}

class B{
 List<C> services;
}    

class C{
  List<String> service;
}
Sign up to request clarification or add additional context in comments.

2 Comments

you are right about my second json code. I will update - forgot the colons. :) and how would this class structure be wired up using (i assume) the jackson mapper?
And how would you propose to solve the problem that url may either be a String or an instance of B?
1

I ended up mixing JsonNode to get this working.

public class Foo {
    @JsonProperty("something")
    private String something;

    @JsonProperty("somethingelse")
    private String somethingelse;

    @JsonProperty("url") 
    JsonNode url;

    // getters setters

    public static Foo parse(String jsonString) {
        ObjectMapper mapper = new ObjectMapper();
        Foo foo = mapper.readValue(jsonString, Foo.class);
        return foo;
    }

    public static boolean validate(Foo foo) {
        JsonNode url = foo.path("url");
        if (url.isTextual()) {
            // this is the first case {"url": "http://something.com"}
            System.out.println(url.getTextValue());
        } else {
            // This is the second case

        }
    }
}

Comments

-1

Answer:

After struggling with Jackson to do what I want in a simple and elegant way, I ended up switching to Gson library for JSON parsing. it allowed me to create a custom deserializer for my class that was extremely easy.

An example of something similar that I did can be found here:

http://www.baeldung.com/gson-deserialization-guide

I appreciate the help and guidance with Jackson, however it just made me realize that Jackson was just not going to meet my needs.

-Stewart

1 Comment

Link-only answers are discouraged on StackOverflow. It is okay to provide a link, but the post here shall still contain a summary of the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.