Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping between JSON objects and Java objects by Codec #1984

Closed
nimo23 opened this issue May 11, 2017 · 1 comment
Closed

Mapping between JSON objects and Java objects by Codec #1984

nimo23 opened this issue May 11, 2017 · 1 comment

Comments

@nimo23
Copy link

nimo23 commented May 11, 2017

According to docs we can map between json and java object by using Jackson’s ObjectMapper#convertValue():

request.bodyHandler(buff -> {
  JsonObject jsonObject = buff.toJsonObject();
  User javaObject = jsonObject.mapTo(User.class);
});

However, I have a "MessageCodec" for "User.class" so the conversion between json and object can be handled by "vertx" instead of "jackson" (faster?). I can not find a method where I can convert it explicitly by using this MessageCodec:

Actually I have do something like this:

UserCodec c = new UserCodec();
String json = c.encodeToWire(Buffer.buffer(), d);
User user =  c.decodeFromWire(0, Buffer.buffer());

Would be nice if vetx can provide something like this:


// use static method of my custom MessageCodec to get JsonObject
JsonObject jsonObject = UserCodec.toJson(User);

// use my custom MessageCodec for conversion
User javaObject = UserCodec.fromJson(json);
@vietj
Copy link
Member

vietj commented Jun 1, 2017

Look at the io.vertx.core.json.Json class, it provides what you want:

JsonObject jsonObject = Json.encode(user);

User javaObject = Json.decodeValue(json, User.class);

@vietj vietj closed this as completed Jun 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants