Issue
so in the long run I'm trying to write a reminder application in Java for my capstone project. Right now I'm trying to write some code to a json file so I can save the information but I've ran into this exception. I have the jar files imported to my project so I'm not sure why it cant seem to access it. Is there anything I can do to fix this?
Heres my java build path for the project. I'm using eclipse and for some reason it has this module path and class path. I don't know the difference between the two but it errors out if I put the jars in the module path.
package util;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonUtil {
private static ObjectMapper mapper;
static {
mapper = new ObjectMapper();
}
public static String convertJavaToJson(Object object)
{
String jsonResult = "";
try {
jsonResult = mapper.writeValueAsString(object);
}
catch (JsonGenerationException e) {
System.out.println("1 Exception Occured while converting Java Object into Json -->" + e.getMessage());
}
catch (JsonMappingException e) {
System.out.println("2 Exception Occured while converting Java Object into Json -->" + e.getMessage());
}
catch (IOException e) {
System.out.println("3 Exception Occured while converting Java Object into Json -->" + e.getMessage());
}
catch (Exception e) {
System.out.println("4 Exception Occured while converting Java Object into Json -->" + e.getMessage());
}
return jsonResult;
}
}
Exception is coming from the final catch block
4 Exception Occured while converting Java Object into Json -->Failed to instantiate standard serializer (of type org.codehaus.jackson.map.ser.std.NullSerializer): class org.codehaus.jackson.map.ser.BasicSerializerFactory cannot access a member of class org.codehaus.jackson.map.ser.std.NullSerializer with modifiers "private"
Any help is appreciated!
Solution
This code works for me but I have this imports :
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
because I use the dependency com.fasterxml.jackson.core:jackson-databind
(version 2.9.9.3).
As said by Lino, the org.codehaus.jackson
is kind of old.
Also, you could change:
private static ObjectMapper mapper;
static {
mapper = new ObjectMapper();
}
by:
private static ObjectMapper mapper = new ObjectMapper();
Answered By - Nicolas Dupouy