2012-02-08 61 views
2

爲了理清在定製序列化器/解串我進行到另一個項目的問題 它犯這樣的錯誤錯誤傑克遜сustom串行器/解串器

ERROR/AndroidRuntime(288):java.lang中.RuntimeException:無法啓動活動ComponentInfo {com。示例/ 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() + ")/"); 
    } 
}  
} 

而當我JsonWrapper.java使用靜態的,而不是公共

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 { 

如果它發生這樣的錯誤

錯誤/ AndroidRuntime(314):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example/com.example.MyActivity}:java.lang.UnsupportedOperationException:無法創建發電機......

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

與你爲什麼要使用SmileFactory錯誤

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

不應該使用TypeToken – 2012-02-08 14:01:47

+0

傑克遜看起來很瘋狂。嘗試谷歌的GSON:http://code.google.com/p/google-gson/ – binnyb 2012-02-08 14:18:33

+0

傑克遜更快捷! – zesen 2012-02-09 07:59:02

回答

1

正如指出的那樣,SmileFactory支持二進制微笑編碼(參見this wiki page),它具有與JSON相同的結構(但更緊湊,處理更快),但不是文本而不是JSON。它必須寫成字節,而不是Java字符。

因此,如果您想要JSON,請使用常規的JsonFactory;或者,如果您確實需要Smile,請提供字節目標:OutputStream是最常見的選擇(如ByteArrayOutputStream)。

+0

非常感謝 – zesen 2012-02-09 07:55:38

2

線?它不支持讀取或寫入非字節目標(在你的情況下你正在嘗試從字符串讀取)。

+0

非常感謝你 – zesen 2012-02-09 07:55:25

相關問題