Tim Willis
Tim Willis

Reputation: 140

working with JSON-Simple in java replacing a value

I'm getting a java safety error and honestly if this was a house I was living in I'd be a little scared too.

obj is a JSONObject from the JSON-Simple library

      obj.put("ord.cancelled_by__c", replaceNull(String.valueOf(((JSONObject)(jsonobjresponseinfo.get("AuditLog"))).get("cancelledBy")), ""));

the auditlog object inside it has a field cancelled by. I would like to check that field for not null and if it is null then pass it "" instead

  private static String replaceNull(String value, String ifnull){
    return replaceNull(value, ifnull, value);
  }
  private static String replaceNull(String value, String ifnull, String ifnotnull){
    return value.isEmpty() ? ifnull : ifnotnull;
  }

to be honest I'm pretty sure this will work but there has to be a more elegant solution. Does anybody have an idea?

I'm recieving a type safety error for raw type hashmap on the put method

Thanks for reading

Upvotes: 0

Views: 70

Answers (1)

serdroid
serdroid

Reputation: 166

Probably you get this error because of your IDE settings. Normally this is a warning. But if you set an option like "treat warnings as error" in your IDE, your IDE complains this as an error. JSONObject class in json-simple library extends java.util.HashMap and does not define generics. So this type of warnings show up.

Upvotes: 1

Related Questions