How to easily convert/parse the Java Object into a JSON object that is a instance com.couchbase.client.java.document.json.JsonObject?
如何轻松地将Java对象转换/解析为一个JSON对象,该对象是一个实例com.couchbase.client.java.document.json.JsonObject?
I tried to do this:
我试着这样做:
import com.couchbase.client.deps.com.fasterxml.jackson.annotation.JsonProperty;
public class MyClass {
@JsonProperty("filed")
private String filed;
public MyClass(String filed) {
this.filed = filed;
}
public String getFiled() {
return filed;
}
and run this lines with imports:
并使用导入运行此行:
import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper;
import com.couchbase.client.java.document.json.JsonObject;
ObjectMapper mapper = new ObjectMapper();
MyClass test = new MyClass("a");
JsonObject node = mapper.convertValue(test, JsonObject.class);
and I get:
我得到:
java.lang.IllegalArgumentException: Unrecognized field "filed" (class com.couchbase.client.java.document.json.JsonObject), not marked as ignorable (one known property: "names"])
at [Source: N/A; line: -1, column: -1] (through reference chain: com.couchbase.client.java.document.json.JsonObject["filed"])
at com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2759)
at com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2685)
2 个解决方案
#1
6
the JsonObject
in Couchbase is meant as a very basic API to work with JSON, close to a Map
: you put
simple values to it, you getString
, getInt
, etc... from it.
Couchbase中的JsonObject是一个非常基本的API,可以使用JSON,靠近Map:你可以将简单的值,getString,getInt等放到它上面。
Note that only a limited set of types are accepted in a JsonObject: null, String, Integer, Long, Double, Boolean, JsonObject or JsonArray.
请注意,JsonObject中只接受一组有限的类型:null,String,Integer,Long,Double,Boolean,JsonObject或JsonArray。
If you want to store domain objects, for now the best supported way is to marshal them to JSON strings (using your prefered flavor of Jackson, GSon, etc...) and store and retrieve them using the RawJsonDocument
.
如果你想存储域对象,现在最好的支持方法是将它们编组为JSON字符串(使用你喜欢的Jackson,GSon等的味道......)并使用RawJsonDocument存储和检索它们。
Example to get a JSON string from the database:
从数据库获取JSON字符串的示例:
RawJsonDocument doc = bucket.get("myKey", RawJsonDocument.class);
String jsonValue = doc.content();
MyClass value = unmarshalToMyClass(jsonValue); //this calls eg. Jackson
edit: the trick below doesn't work that great (eg. problem converting longs) but here's a trick to do what you want to do: there's a preconfigured Jackson ObjectMapper
that you can use in JacksonTransformers.MAPPER
!
编辑:下面的技巧不是那么好(例如转换longs的问题)但这里有一个做你想做的事情的技巧:有一个预配置的Jackson ObjectMapper,你可以在JacksonTransformers.MAPPER中使用!
#2
0
I made a very simple library on top of the Java 2.x Couchbase Client which allows to perform CRUD operations synchronously, as well as asynchronously (through RxJava).
我在Java 2.x Couchbase客户端上创建了一个非常简单的库,它允许同步执行CRUD操作,也可以异步执行(通过RxJava)。
It shows how to use Jackson to serialize / deserialize the beans stored on Couchbase.
它显示了如何使用Jackson来序列化/反序列化存储在Couchbase上的bean。
The project on GitHub: https://github.com/jloisel/reactive-couchbase
GitHub上的项目:https://github.com/jloisel/reactive-couchbase
#1
6
the JsonObject
in Couchbase is meant as a very basic API to work with JSON, close to a Map
: you put
simple values to it, you getString
, getInt
, etc... from it.
Couchbase中的JsonObject是一个非常基本的API,可以使用JSON,靠近Map:你可以将简单的值,getString,getInt等放到它上面。
Note that only a limited set of types are accepted in a JsonObject: null, String, Integer, Long, Double, Boolean, JsonObject or JsonArray.
请注意,JsonObject中只接受一组有限的类型:null,String,Integer,Long,Double,Boolean,JsonObject或JsonArray。
If you want to store domain objects, for now the best supported way is to marshal them to JSON strings (using your prefered flavor of Jackson, GSon, etc...) and store and retrieve them using the RawJsonDocument
.
如果你想存储域对象,现在最好的支持方法是将它们编组为JSON字符串(使用你喜欢的Jackson,GSon等的味道......)并使用RawJsonDocument存储和检索它们。
Example to get a JSON string from the database:
从数据库获取JSON字符串的示例:
RawJsonDocument doc = bucket.get("myKey", RawJsonDocument.class);
String jsonValue = doc.content();
MyClass value = unmarshalToMyClass(jsonValue); //this calls eg. Jackson
edit: the trick below doesn't work that great (eg. problem converting longs) but here's a trick to do what you want to do: there's a preconfigured Jackson ObjectMapper
that you can use in JacksonTransformers.MAPPER
!
编辑:下面的技巧不是那么好(例如转换longs的问题)但这里有一个做你想做的事情的技巧:有一个预配置的Jackson ObjectMapper,你可以在JacksonTransformers.MAPPER中使用!
#2
0
I made a very simple library on top of the Java 2.x Couchbase Client which allows to perform CRUD operations synchronously, as well as asynchronously (through RxJava).
我在Java 2.x Couchbase客户端上创建了一个非常简单的库,它允许同步执行CRUD操作,也可以异步执行(通过RxJava)。
It shows how to use Jackson to serialize / deserialize the beans stored on Couchbase.
它显示了如何使用Jackson来序列化/反序列化存储在Couchbase上的bean。
The project on GitHub: https://github.com/jloisel/reactive-couchbase
GitHub上的项目:https://github.com/jloisel/reactive-couchbase