我已经读过,Smile和JSON之间的转换可以在几个来源中有效地完成:
甚至Wikipedia: ...这意味着使用JSON的工具也可以与Smile一起使用,只要存在适当的编码器/解码器供工具使用 < / p>
不幸的是,除了有关编码器/解码器的信息外,我在任何来源都没有找到任何帮助。
所以一般的问题是如何做到这一点?
答案 0 :(得分:3)
public class JsonSmileMigrationService
{
private static final Logger log = LoggerFactory.getLogger(JsonSmileMigrationService.class);
public static byte[] convertToSmile(byte[] json, JsonFactory jsonFactory, SmileFactory smileFactory)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try // try-with-resources
(
JsonGenerator jg = smileFactory.createGenerator(bos);
JsonParser jp = jsonFactory.createParser(json)
)
{
while (jp.nextToken() != null)
{
jg.copyCurrentEvent(jp);
}
}
catch (Exception e)
{
log.error("Error while converting json to smile", e);
}
return bos.toByteArray();
}
public static byte[] convertToJson(byte[] smile, JsonFactory jsonFactory, SmileFactory smileFactory)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try // try-with-resources
(
JsonParser sp = smileFactory.createParser(smile);
JsonGenerator jg = jsonFactory.createGenerator(bos)
)
{
while (sp.nextToken() != null)
{
jg.copyCurrentEvent(sp);
}
}
catch (Exception e)
{
log.error("Error while converting smile to json", e);
}
return bos.toByteArray();
}
}
答案 1 :(得分:2)
我无法评估这个MO是多么有效,但是,这个简单直接的方法可以正常工作:从SmileFactory创建一个jackson映射器,给它一个JsonNode实例并让它将输入映射到byte []微笑数据:
public static void main(String[] args)
{
ObjectMapper jsonMapper = new ObjectMapper(); // just for testing!
ObjectMapper smileMapper = new ObjectMapper(new SmileFactory());
// create json test data
SmileDemo sd = new SmileDemo();
System.out.println("ORIGINAL " + sd.str + " " + sd.i + " " + sd.flt + " " + sd.dbl + " " + sd.bool + " " + sd.date);
JsonNode json = jsonMapper.valueToTree(sd);
try {
// the following line is the actual conversion
byte[] smileData = smileMapper.writeValueAsBytes(json);
// test the conversion
sd = smileMapper.readValue(smileData, SmileDemo.class);
System.out.println("CONVERTED " + sd.str + " " + sd.i + " " + sd.flt + " " + sd.dbl + " " + sd.bool + " " + sd.date);
} catch (Exception e) {
e.printStackTrace();
}
}
public static class SmileDemo
{
public String str = "Hello World";
public int i = 1000;
public float flt = 1000.34F;
public double dbl = 1000.34D;
public boolean bool = false;
public java.util.Date date = new Date(2016, 1, 1);
}
答案 2 :(得分:1)
谷歌找到我这个class,其中包含从json字节数组到微笑数组的转换(搜索convertToSmile
)。也许这有帮助?
答案 3 :(得分:0)
没有直接转码,但相对有效的方法是使用特定格式的映射器读入JsonNode
;使用“另一个”映射器写出。像这样:
final SmileMapper SMILE_MAPPER = new SmileMapper();
final JsonMapper JSON_MAPPER = new JsonMapper();
public byte[] smileBytesFromJson(byte[] json) throws IOException
{
JsonNode tree = JSON_MAPPER.readTree(json);
return SMILE_MAPPER.writeValueAsBytes(tree);
}
public byte[] jsonBytesFromSmile(byte[] smile) throws IOException
{
JsoNode tree = SMILE_MAPPER.readTree(smile);
return JSON_MAPPER.writeValueAsBytes(tree);
}
// can also take/produce String for JSON
但如果性能真的很重要,您可以替代使用已接受答案所示的 JsonParser
/JsonGenerator
方法;想法是正确的,创建SmileFactory
,JsonFactory
,解析器/生成器对,迭代令牌,使用JsonGenerator.copyCurrentEvent(sourceParser);