2017-06-19 144 views
0

有没有办法将一般对象序列化为Json并将Json反序列化为对象?
如果对象是一个实体,我可以使用Jackson 2 lib来达到目的。
但是,如果对象是一个普通的类,我该怎么做?如何将一般对象序列化为Json并将Json反序列化为Java中的对象

例如,我想序列com.datastax.driver.core.querybuilder.Update到JSON并将其保存到数据库,然后搜索并反序列化到Update对象,最后把它作为com.datastax.driver.core.Session.execute(Statement)的参数execuate。

是否有可能重现Update对象?

我真正的目的是存储Update,然后检索并执行它。所以我想我可以将它保存为JSON String,然后检索并反序列化到原始的Update实例并执行它。如果这不是一个好方法,我该如何储存Update然后检索并执行它?
虽然我可以存储text queryUpdate并检索并执行text query,但Update中的某些其他信息可能会丢失,如ConsistencyLevel

回答

0

只是转换了QueryBuilder.update到使用toString()方法

如果要存储,检索和执行QueryBuilder.update查询字符串,你不必序列化和反序列化成JSON。

String query = QueryBuilder.update("exp").with(QueryBuilder.set("data", "This is a test data")).toString(); 
//Now you can store the text query directly to cassandra 
//And retrieve the text query 
session.execute(query); //Execute the query 

这里是toString()

@Override 
public String toString() { 
    try { 
     if (forceNoValues) 
      return getQueryString(); 
     // 1) try first with all values inlined (will not work if some values require custom codecs, 
     // or if the required codecs are registered in a different CodecRegistry instance than the default one) 
     return maybeAddSemicolon(buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE)).toString(); 
    } catch (RuntimeException e1) { 
     // 2) try next with bind markers for all values to avoid usage of custom codecs 
     try { 
      return maybeAddSemicolon(buildQueryString(new ArrayList<Object>(), CodecRegistry.DEFAULT_INSTANCE)).toString(); 
     } catch (RuntimeException e2) { 
      // Ugly but we have absolutely no context to get the registry from 
      return String.format("built query (could not generate with default codec registry: %s)", e2.getMessage()); 
     } 
    } 
} 

的代码,你可以看到,它返回的实际查询字符串

+0

是的,我想存储,检索并执行'com.datastax.driver.core.querybuilder.Update'查询。 你的意思是使用'toString()'方法可以得到正确的文本查询,然后直接执行它? 您确定这是**是否正确**复杂**更新操作? 我不知道'toString()'方法到底做了什么。源代码对我来说有点困难。 – niaomingjian

+0

尽管我可以获取文本查询,但我可能会在'Update'中丢失一些其他信息。 – niaomingjian

+0

@niaomingjian更新了答案,添加了'toString()'代码。你可以看到它返回实际的查询字符串。 –

0

最坏的情况下,你可以总是看看创建你的自己的自定义(德)序列化代码。

请参阅herethere以供进一步阅读。

含义:你可以看看你想要“序列化”的类的实现;然后编写适用于该实现的代码。

这样做对于第三方代码的主要缺点是:您现在必须跟踪对第三方代码的更改;作为对更新类的更改,可能会轻易破坏您的自定义(de)序列化程序代码。

+0

我真正的目的是存储'Update',然后检索并执行它。所以我想知道我是否可以使用Json格式来存储它。还有其他方法可以做到吗? – niaomingjian

+0

我给你提供了描述如何存储任意类的对象的输入。我真的不明白你的意思。这里没有魔法。 *您必须查看您想要序列化的类的实现,然后编写代码。我可以考虑的唯一选择是:Google gson使用反射,所以它不需要Jackson期望的getter/setter方法。但是这听起来像是你负担过重了。最后:你会发现所有的答案都值得“重复”你的问题,但是他们没有一个足以胜任你的问题吗?非常激励... – GhostCat

0

org.json提供了一个操纵json的简单API。 有一种方法根据其getter创建类实例的JSONObject。 例如:

public class MyClass{ 
    private int id; 
    private String name, 

    public MyClass(int id_, String name_) { 
     this.id = id_; 
     this.name = name_; 
    } 

    public int getId() { return this.id; } 
    public int getName() { return this.name; } 

    //Create a JSONObject with a key-value for each getter of your class 
    public JSONObject toJson(){ 
     return new JSONObject(this); 
    } 
} 

然后,在你的代码:

MyClass obj = new MyClass(1, "doe"); 
JSONObject json = obj.toJson(); 
System.out.println(json.toString()); 
// -> print : {"id":1,"name":"doe"} 

的API还提供型动物解析器,你可以编写与一个JSONObject类的构造函数中提供:

public MyClass(JSONObject json){ 
    this.id= json.getInt("id"); 
    this.name= json.getString("name"); 
} 

完整文档在:org.json official doc

+0

我真正的目的是存储'Update',然后检索并执行它。 'Update'是第三方代码,不是我的。 – niaomingjian

+0

你的'Update'对象是否包含getters?如果是这样,我的方法仍然有效。 – Asew