JestResult can't parse my custom JsonObject - java

I am trying to write unit tests for my JestClient wrapper. To do so, I created a JestResult object where I set a custom JsonObject that has my expected output. However, the JestResult cannot parse my JsonObject. The issue is in the following JestResult method:
protected <T> T createSourceObject(JsonElement source, Class<T> type) {
T obj = null;
try {
String json = source.toString();
obj = gson.fromJson(json, type);
// Check if JestId is visible
Field[] fields = type.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(JestId.class)) {
try {
field.setAccessible(true);
Object value = field.get(obj);
if (value == null) {
Class<?> fieldType = field.getType();
JsonElement id = ((JsonObject) source).get(ES_METADATA_ID);
field.set(obj, getAs(id, fieldType));
}
} catch (IllegalAccessException e) {
log.error("Unhandled exception occurred while getting annotated id from source");
}
break;
}
}
} catch (Exception e) {
log.error("Unhandled exception occurred while converting source to the object ." + type.getCanonicalName(), e);
}
return obj;
}
Specifically, the line
obj = gson.fromJson(json, type);
This line sets all fields of obj to null. However, if I use the same line in my own code, all fields are properly initialized. I am using
Jest: 0.1.6
Gson: 2.3.1

Related

Null while getting string from json file

I am using json simple
this is my code:
public static String getDetails() {
String name = System.getProperty("user.name");
JSONParser parser = new JSONParser();
File dir = new File("C:\\Users\\" + name + "\\AppData\\Roaming\\.minecraft\\launcher_profiles.json");
if (dir.exists()) {
Object obj = null;
try {
obj = parser.parse(new FileReader("C:\\Users\\" + name + "\\AppData\\Roaming\\.minecraft\\launcher_profiles.json"));
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
String da = (String) jsonObject.get("username");
try {
return obj.toString() + "\n" + da;
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("dir no exist");
}
return null;
}
when i print this out it returns all the text in the json file and it returns null from String 'da' I dont know why because its not null it exist in the file??
JSON file: https://hastebin.com/sirefacado.json
To access the username you have to use the fully qualified path. In your case that is:
String da = (String) ((JSONObject)((JSONObject)jsonObject.get("authenticationDatabase")).get("d46e53840f3f41a2b9e44e2d4d72ebeb")).get("username");
That is, because your username is encapsulated in the following part of the JSON file:
authenticationDatabase: {
d46e53840f3f41a2b9e44e2d4d72ebeb: {
accessToken: "86ccdfsdfsdfsc2c38ec6012a1ccfsdfR",
username: "privater#email.co",
profiles: {
ad4fa7102fb7432cb4e07d471e348c77: {
displayName: "hio"
}
}
}
}
To access the username via the token you have to go via the authenticationDatabase. It may be the case that there are multiple ids, therefore you have to iterate over all the existing ones
For that you can do
JSONObject authDatabase = (JSONObject) jsonObject.get("authenticationDatabase");
for(Object id : authDatabase.keySet()) {
JSONObject authEntry = (JSONObject) authDatabase.get(id);
String username = (String) authDatabase.get("username");
/* now do something with the username.
You can abort after you found the first username
and store it in the da object, or create a list
of existing usernames, ... */
}
Following are the top level keys in the JSON you posted.
{
"settings": {...some data...},
"launcherVersion": {...some data...},
"clientToken": "dbf69db062d5d32b093e7d67ce744d60",
"profiles": {...some data...},
"analyticsFailcount": 0,
"analyticsToken": "f18d7c0f152f5ad44b2a6525e0d5cfa9",
"selectedProfile": "OptiFine",
"authenticationDatabase": {...some data...},
"selectedUser": {...some data...}
}
Your code tries to extract the value of username from the top level.
String da = (String) jsonObject.get("username")
It doesn't contain the key username. Hence, it is print null.
Your statement below is trying to fetch an element named "username" from the root JSON object however, your actual value is nested inside.
String da = (String) jsonObject.get("username");
{
"authenticationDatabase": {
"d46e53840f3f41a2b9e44e2d4d72ebeb": {
"accessToken": "86ccdfsdfsdfsc2c38ec6012a1ccfsdfR",
"username": "privater#email.co",
"profiles": {
"ad4fa7102fb7432cb4e07d471e348c77": {
"displayName": "hio"
}
}
}
}
}
In order to fetch inner element, you need to drill down as follows. Understand that it is not an good idea to hard cord the key inside the "authenticationDatabase" object.
JSONObject jsonObject = (JSONObject) obj;
String da;
try {
JSONObject adb = (JSONObject) jsonObject.get("authenticationDatabase");
JSONObject adbKey = null;
for(Object key:adb.keySet()) {
String sKey = (String) key;
adbKey = (JSONObject) adb.get(sKey);
da = (String) adbKey.get("username");
return obj.toString() + "\n" + da;
}
} catch (Exception e) {
e.printStackTrace();
}

Java to Json validation using GSON

While converting Java object to Json string using GSON API, I also want to fail this Json conversion if any of the annotated attribute is null.
For example
public class Order{
#SerializedName("orderId")
#Expose
#Required
private Integer id;
//getter & setter available for id
}
Now as I am doing
Order order = new Order();
JSONObject jsonobj = new JSONObject(gson.toJson(order));
I want to fail the above Java to Json transformation if any of the #Required attribute is null
Is this possible using GSON?
I wanted to fail Java to Json conversion, if any of the Java attribute is null which is annotated as #Required,
I am able to achieve this using following approach. Please let me know if you see any issues:
class RequiredKeyAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
#Override
public void write(JsonWriter out, T value) throws IOException {
if (value != null) {
Field[] fields = value.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i]
.isAnnotationPresent(Required.class)) {
validateNullValue(value, fields[i]);
}
}
}
delegate.write(out, value);
}
private <T> void validateNullValue(T value, Field field) {
field.setAccessible(true);
Class t = field.getType();
Object v = null;
try {
v = field.get(value);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
if (t == boolean.class && Boolean.FALSE.equals(v)) {
throw new IllegalArgumentException(field + " is null");
} else if (t.isPrimitive()
&& ((Number) v).doubleValue() == 0) {
throw new IllegalArgumentException(field + " is null");
} else if (!t.isPrimitive() && v == null) {
throw new IllegalArgumentException(field + " is null");
}
}
#Override
public T read(JsonReader in) throws IOException {
return delegate.read(in);
}
};
}
}
RequiredKeyAdapterFactory requiredKeyAdapterFactory = new RequiredKeyAdapterFactory();
Gson gson = new GsonBuilder().registerTypeAdapterFactory(requiredKeyAdapterFactory)
.create();
This is working

Parsing single-quote JSON String field

Basically, I'm trying to do this:
#Test
public void testCreateFromString() {
BasicDBObject obj = new BasicDBObject("{'username': 'xirby'}");
assertNotNull(obj.get("username"));
}
#Test
public void testCreateQueryFromString() {
BasicDBObject query = new BasicDBObject("{'$inc': {number: 1}}");
assertNotNull(query.get("$inc"));
}
Both test fails, with error:
Unexpected character (') at position 2.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
BasicDBObject.java: (w/c is a HashMap)
public BasicDBObject(String doc){
try {
JSONParser parser=new JSONParser();
Object obj = parser.parse(doc);
if (obj != null){
if (obj instanceof JSONObject){
putAll((Map<String,Object>) obj);
}
} else {
throw new RuntimeException("Cannot parse document: " + doc);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Its most likely that the JSON String would be like this on code to eliminate the need to \ escape character:
"{'username' : 'xirby'}"
So are there any JSON parser that could parse this?
Or
Perhaps, a library that can turn this String into a valid JSON String.

IllegalArgumentException JSON

I am trying to put String[] in jsonObject and getting following error
java.lang.IllegalArgumentException: Invalid type of value. Type:
[[Ljava.lang.String;] with value: [[Ljava.lang.String;#189db56] at
com.ibm.json.java.JSONObject.put(JSONObject.java:241)
Please help me to resolve this.
Thanks
public JSONObject toJSONObject() {
JSONObject jsonObject = new JSONObject();
//Use reflection to get a list of all get methods
//and add there corresponding values to the JSON object
Class cl = dto.getClass();
logger.infoFormat("Converting {0} to JSON Object", cl.getName());
Method[] methods = cl.getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get")) {
logger.infoFormat("Processing method - {0}", methodName);
//Check for no parameters
if (method.getParameterTypes().length == 0) {
String tag = getLabel(method);
Object tagValue = new Object();
try {
tagValue = method.invoke(dto);
} catch (Exception e) {
logger.errorFormat("Error invoking method - {0}", method.getName());
}
if (method.getReturnType().isAssignableFrom(BaseDTO.class)) {
DTOSerializer serializer = new DTOSerializer((BaseDTO) tagValue);
jsonObject.put(tag, serializer.toJSONObject());
} else if (method.getReturnType().isAssignableFrom(List.class)) {
ListSerializer serializer = new ListSerializer((List<BaseDTO>) tagValue);
jsonObject.put(tag, serializer.toJSONArray());
} else {
if (tagValue != null) jsonObject.put(tag, tagValue);
}
}
}
}
return(jsonObject);
}
try
jsonObject.put("yourKey", Arrays.asList(yorStringArray));
As you should read the manual first http://www.json.org/javadoc/org/json/JSONObject.html there is no variation of it expects an Object[]
Maybe you should take a look at google-gson.
I like it very much to work with json in Java.

Jackson deserialization error handling

My problem is fairly simple : I have the following simple class:
public class Foo {
private int id = -1;
public void setId(int _id){ this.id = _id; }
public int getId(){ return this.id; }
}
And I am trying to process following JSON:
{
"id": "blah"
}
Obviously, there is a problem here ("blah" cannot be parsed to an int)
Formerly, Jackson throws something like org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Integer from String value 'blah': not a valid Integer value
I agree with this, but I'd like to register something somewhere allowing to ignore this type of mapping errors.
I tried with a DeserializationProblemHandler registered (see here) but it seems to only work on unknown properties and not deserialization problems.
Have you any clue on this issue?
I succeeded to solve my problem, thanks to Tatu from Jackson ML.
I had to use custom non blocking deserializers for every primitive types handled in Jackson.
Something like this factory :
public class JacksonNonBlockingObjectMapperFactory {
/**
* Deserializer that won't block if value parsing doesn't match with target type
* #param <T> Handled type
*/
private static class NonBlockingDeserializer<T> extends JsonDeserializer<T> {
private StdDeserializer<T> delegate;
public NonBlockingDeserializer(StdDeserializer<T> _delegate){
this.delegate = _delegate;
}
#Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
try {
return delegate.deserialize(jp, ctxt);
}catch (JsonMappingException e){
// If a JSON Mapping occurs, simply returning null instead of blocking things
return null;
}
}
}
private List<StdDeserializer> jsonDeserializers = new ArrayList<StdDeserializer>();
public ObjectMapper createObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule customJacksonModule = new SimpleModule("customJacksonModule", new Version(1, 0, 0, null));
for(StdDeserializer jsonDeserializer : jsonDeserializers){
// Wrapping given deserializers with NonBlockingDeserializer
customJacksonModule.addDeserializer(jsonDeserializer.getValueClass(), new NonBlockingDeserializer(jsonDeserializer));
}
objectMapper.registerModule(customJacksonModule);
return objectMapper;
}
public JacksonNonBlockingObjectMapperFactory setJsonDeserializers(List<StdDeserializer> _jsonDeserializers){
this.jsonDeserializers = _jsonDeserializers;
return this;
}
}
Then calling it like this way (pass as deserializers only those you want to be non blocking) :
JacksonNonBlockingObjectMapperFactory factory = new JacksonNonBlockingObjectMapperFactory();
factory.setJsonDeserializers(Arrays.asList(new StdDeserializer[]{
// StdDeserializer, here, comes from Jackson (org.codehaus.jackson.map.deser.StdDeserializer)
new StdDeserializer.ShortDeserializer(Short.class, null),
new StdDeserializer.IntegerDeserializer(Integer.class, null),
new StdDeserializer.CharacterDeserializer(Character.class, null),
new StdDeserializer.LongDeserializer(Long.class, null),
new StdDeserializer.FloatDeserializer(Float.class, null),
new StdDeserializer.DoubleDeserializer(Double.class, null),
new StdDeserializer.NumberDeserializer(),
new StdDeserializer.BigDecimalDeserializer(),
new StdDeserializer.BigIntegerDeserializer(),
new StdDeserializer.CalendarDeserializer()
}));
ObjectMapper om = factory.createObjectMapper();
You might want to let your controller handle the problem by adding a method that handles this specific exception
#ExceptionHandler(HttpMessageNotReadableException.class)
#ResponseBody
public String handleHttpMessageNotReadableException(HttpMessageNotReadableException ex)
{
JsonMappingException jme = (JsonMappingException) ex.getCause();
return jme.getPath().get(0).getFieldName() + " invalid";
}
Of course, the line
JsonMappingException jme = (JsonMappingException) ex.getCause();
might throw a class cast exception for some cases but i haven't encountered them yet.
I have written a simple error handler which will give you some kind of error which you can return to user with bad request as status code. Use #JsonProperty required = true to get error related to missing properties. Jackson version 2.9.8.
public class JacksonExceptionHandler {
public String getErrorMessage(HttpMessageNotReadableException e) {
String message = null;
boolean handled = false;
Throwable cause = e.getRootCause();
if (cause instanceof UnrecognizedPropertyException) {
UnrecognizedPropertyException exception = (UnrecognizedPropertyException) cause;
message = handleUnrecognizedPropertyException(exception);
handled = true;
}
if (cause instanceof InvalidFormatException) {
InvalidFormatException exception = (InvalidFormatException) cause;
message = handleInvalidFormatException(exception);
handled = true;
}
if (cause instanceof MismatchedInputException) {
if (!handled) {
MismatchedInputException exception = (MismatchedInputException) cause;
message = handleMisMatchInputException(exception);
}
}
if (cause instanceof JsonParseException) {
message = "Malformed json";
}
return message;
}
private String handleInvalidFormatException(InvalidFormatException exception) {
String reference = null;
if (!exception.getPath().isEmpty()) {
String path = extractPropertyReference(exception.getPath());
reference = removeLastCharacter(path);
}
Object value = exception.getValue();
return "Invalid value '" + value + "' for property : " + reference;
}
private String handleUnrecognizedPropertyException(UnrecognizedPropertyException exception) {
String reference = null;
if (!exception.getPath().isEmpty()) {
String path = extractPropertyReference(exception.getPath());
reference = removeLastCharacter(path);
}
return "Unknown property : '" + reference + "'";
}
private String handleMisMatchInputException(MismatchedInputException exception) {
String reference = null;
if (!exception.getPath().isEmpty()) {
reference = extractPropertyReference(exception.getPath());
}
String property = StringUtils.substringBetween(exception.getLocalizedMessage(), "'", "'");
// if property missing inside nested object
if (reference != null && property!=null) {
return "Missing property : '" + reference + property + "'";
}
// if invalid value given to array
if(property==null){
return "Invalid values at : '"+ reference +"'";
}
// if property missing at root level
else return "Missing property : '" + property + "'";
}
// extract nested object name for which property is missing
private String extractPropertyReference(List<JsonMappingException.Reference> path) {
StringBuilder stringBuilder = new StringBuilder();
path.forEach(reference -> {
if(reference.getFieldName() != null) {
stringBuilder.append(reference.getFieldName()).append(".");
// if field is null means it is array
} else stringBuilder.append("[].");
}
);
return stringBuilder.toString();
}
// remove '.' at the end of property path reference
private String removeLastCharacter(String string) {
return string.substring(0, string.length() - 1);
}
}
and call this class object in global advice like this
#Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
String message = new JacksonExceptionHandler().generator.getErrorMessage(ex);
if(message == null){
return ResponseEntity.badRequest().body("Malformed json");
}
return ResponseEntity.badRequest().body(message);
}
Create a simple Mapper:
#Provider
#Produces(MediaType.APPLICATION_JSON)
public class JSONProcessingErroMapper implements ExceptionMapper<InvalidFormatException> {
#Override
public Response toResponse(InvalidFormatException ex) {
return Response.status(400)
.entity(new ClientError("[User friendly message]"))
.type(MediaType.APPLICATION_JSON)
.build();
}
}
DeserializationProblemHandler now has a lot more methods, such as handleUnexpectedToken and handleWeird*Value. It should be able to handle anything one needs.
Subclass it, override methods you need, then add it to your ObjectMapper with addHandler(DeserializationProblemHandler h).

Categories