Jackson自定义序列化器/反序列化器出错

时间:2012-02-08 13:59:24

标签: java android jackson

为了解决自定义序列化器/反序列化器中的问题,我将其执行到另一个项目 它犯了这样的错误

错误/ AndroidRuntime(288):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example / com.example.MyActivity}:java.lang.UnsupportedOperationException:无法为非基于字节的目标创建生成器

http://i.imgur.com/hOFYI.jpg

程序文件:

MyActivity.java

package com.example;

import android.app.Activity;
import android.os.Bundle;
import com.example.JacksonObject;
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.JsonGenerationException;
import java.io.IOException;

public class MyActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String jacksonString = "{\"DateField\":\"/Date(61283424600000)/\",\"StringField\":\"STRING_string\",\"DoubleField\":\"87.12345\",\"IntegerField\":\"387\"}";
try {
    MyJsonWrapper sss = new MyJsonWrapper();

    JacksonObject[] mailItems2 = sss.getMyJson().readValue(jacksonString, JacksonObject[].class);
    int a2 = 3;  //это просто так, что бы поставить точки!!!
    int  b = a2; //это просто так, что бы поставить точки!!!

}   catch (JsonGenerationException e) {

    e.printStackTrace();

} catch (JsonMappingException e) {

    e.printStackTrace();

} catch (IOException e) {

    e.printStackTrace();

}
int a = 3; //это просто так, что бы поставить точки!!!
int s = 10; //это просто так, что бы поставить точки!!!
s = s + a; //это просто так, что бы поставить точки!!!
}
}

JacksonObject.java

package com.example;

import java.util.Date;

public class JacksonObject
{
public Date DateField;
public String StringField;
public Double DoubleField;
public int IntegerField;
}

MyJsonWrapper.java

package com.example;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.smile.*;
import org.codehaus.jackson.*;
import org.codehaus.jackson.map.ser.*;
import org.codehaus.jackson.map.ser.std.NullSerializer;
import org.codehaus.jackson.map.deser.*;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date;

public class MyJsonWrapper
{

public ObjectMapper getMyJson()
{
ObjectMapper mapper = new ObjectMapper(new SmileFactory());        
SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));

module.addSerializer(Date.class, new JsonDateSerializer());
module.addDeserializer(Date.class, new JsonDateDeserializer());

mapper.registerModule(module);

return mapper;
}

public class JsonDateDeserializer extends JsonDeserializer<Date>
{

public Date deserialize(JsonParser jp, DeserializationContext context) throws IOException, JsonProcessingException
{
    try {

    String s = jp.getText().replace("/Date(", "").replace(")/", "");

    if (s.equals("")) return null;

    boolean isDateBefore1970 = false;

    ............................................

    if (isDateBefore1970)
        return new Date(-Long.valueOf(s) - offset * 60 * 1000);
    else
        return new Date(Long.valueOf(s) + offset * 60 * 1000);

    }catch (JsonMappingException e){
        // If a JSON Mapping occurs, simply returning null instead of blocking things
        return null;
    }

  }
  }

  public class JsonDateSerializer extends JsonSerializer<Date>
 {
   public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider)   throws IOException, JsonProcessingException
  {            
    jgen.writeString("/Date(" + date.getTime() + ")/");
  }
 }    
 }

并且在My JsonWrapper.java中使用static而不是public

 public class MyJsonWrapper
{
 public static ObjectMapper getMyJson()
{        
ObjectMapper mapper = new ObjectMapper(new SmileFactory());        
SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));

module.addSerializer(Date.class, new JsonDateSerializer());
module.addDeserializer(Date.class, new JsonDateDeserializer());

mapper.registerModule(module);

return mapper;
}

static class JsonDateDeserializer extends JsonDeserializer<Date>
{

public Date deserialize(JsonParser jp, DeserializationContext context) throws IOException, JsonProcessingException
{
    try {

它犯了这样的错误

ERROR / AndroidRuntime(314):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example / com.example.MyActivity}:java.lang.UnsupportedOperationException:无法为......创建生成器

http://i.imgur.com/dohnz.jpg

与错误一致

JacksonObject[] mailItems2 = sss.getMyJson().readValue(jacksonString, JacksonObject[].class);

2 个答案:

答案 0 :(得分:2)

您为什么使用SmileFactory?它不支持读取或写入非字节目标(在您尝试从字符串中读取的情况下)。

答案 1 :(得分:2)

正如所指出的,SmileFactory支持二进制Smile编码(参见this wiki page),它具有与JSON相同的结构(但更紧凑,处理速度更快),但不是文本而不是JSON。它必须写为字节,而不是Java字符。

编辑:2018-06-19 - 所有二进制格式编解码器都是如此 - Avro,CBOR,Protobuf(以及通过bson4jackson的BSON,通过jackson-dataformat-msgpack的MsgPack),而不仅仅是Smile。

如果你想要JSON,那么使用常规的JsonFactory;或者,如果你想要微笑,提供字节目标:OutputStream是最常见的选择(如ByteArrayOutputStream)。