com.google.common.base.Optional 是否有 Json 序列化器/反序列化器?
开箱即用,这似乎不适用于杰克逊,见下文:
package com.example;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.google.common.base.Optional;
public class TestClass {
public Optional<String> myString;
public TestClass() {
myString = Optional.of("testString");
}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
TestClass testClass = new TestClass();
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(testClass);
System.out.println(jsonString);
}
}
-> {"myString":{"present":true}}