0

I'm making something that deals with JSON in processing. The input format is flexible so I need to do the same code for different classes often. Is there any preferred way to do this other that what I've done?

Object part = json.get(0);
File saveTo = new File(dataPath("test.txt"));
if (part.getClass() == JSONObject.class)
  ((JSONObject)part).save(saveTo, "");
if (part.getClass() == JSONArray.class)
  ((JSONArray)part).save(saveTo, "");
6
  • Why do you need the cast? toString is a method of Object Commented Apr 9, 2017 at 10:58
  • 1
    Write the code once for an ancestor of all the classes that has the method in question. Commented Apr 9, 2017 at 11:00
  • @UnholySheep Well guess that was a bad example. Didn't think about that. Commented Apr 9, 2017 at 11:02
  • Not directly an answer (I'd need a better example of what's your real problem), but if you're dealing with JSON a lot I'd strongly recommend having a look at the Jackson library (github.com/FasterXML/jackson). By adding some annotations to Java classes it can automatically encode/decode JSON into Java objects and back. Commented Apr 9, 2017 at 11:04
  • @Ede I'm using Processing which has JSON stuff built in. Does that deal well with mixed type stuff and incorrect JSON (like {a:"b",c:[{"test":hello world}]}?) Commented Apr 9, 2017 at 11:11

1 Answer 1

1

Couple things:

First off, you should always wrap if statements in { } curly brackets.

Object part = json.get(0);
File saveTo = new File(dataPath("test.txt"));
if (part.getClass() == JSONObject.class){
  ((JSONObject)part).save(saveTo, "");
}
if (part.getClass() == JSONArray.class){
  ((JSONArray)part).save(saveTo, "");
}

Secondly, if you only expect one of those if statements to execute, then you should probably use an else if:

Object part = json.get(0);
File saveTo = new File(dataPath("test.txt"));
if (part.getClass() == JSONObject.class){
  ((JSONObject)part).save(saveTo, "");
}
else if (part.getClass() == JSONArray.class){
  ((JSONArray)part).save(saveTo, "");
}

Third, you could use the instanceof operator instead of getting the class:

Object part = json.get(0);
File saveTo = new File(dataPath("test.txt"));
if (part instanceof JSONObject){
  ((JSONObject)part).save(saveTo, "");
}
else if (part instanceof JSONArray){
  ((JSONArray)part).save(saveTo, "");
}

But to answer your question, there isn't a great way to minimize this code, because JSONObject and JSONArray are both direct subclasses of Object. It would be easier if, say, JSONArray was a subclass of JSONObject, but it's not.

But if you find yourself writing this code in multiple places, you should extract it into a function, like this:

void saveJsonThing(Object part, File file)
    if (part instanceof JSONObject){
      ((JSONObject)part).save(saveTo, "");
    }
    else if (part instanceof JSONArray){
      ((JSONArray)part).save(saveTo, "");
    }
    else{
       //handle error?
    }
}

Then your code can just call that function whenever it needs to save something:

Object part = json.get(0);
File saveTo = new File(dataPath("test.txt"));
saveJsonThing(part, saveTo);
Sign up to request clarification or add additional context in comments.

Comments

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.