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

deserialize from Json string to Message object #100

Closed
hamata5656 opened this issue Apr 10, 2017 · 8 comments · Fixed by #108
Closed

deserialize from Json string to Message object #100

hamata5656 opened this issue Apr 10, 2017 · 8 comments · Fixed by #108

Comments

@hamata5656
Copy link
Contributor

I want to deserialize from Json string to Message object, but is there something better?

@be-hase
Copy link
Member

be-hase commented Apr 10, 2017

How about this ?
We support JSON-deserialize by Jackson.
( https://github.com/FasterXML/jackson )

final ObjectMapper objectMapper = new ObjectMapper();
final CallbackRequest callbackRequest = objectMapper.readValue(json, CallbackRequest.class);

ref:
https://github.com/line/line-bot-sdk-java/blob/master/line-bot-servlet/src/main/java/com/linecorp/bot/servlet/LineBotCallbackRequestParser.java

@hamata5656
Copy link
Contributor Author

Can not deserialize.

String json = "{\"type\":\"text\",\"text\":\"Hello, world\"}";
final ObjectMapper objectMapper = new ObjectMapper();
final Message message = objectMapper.readValue(json, Message.class);
	
 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "type" (class com.linecorp.bot.model.message.TextMessage), not marked as ignorable (one known property: "text"])
 at [Source: {"type":"text","text":"Hello, world"}; line: 1, column: 38] (through reference chain: com.linecorp.bot.model.message.TextMessage["type"])
	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
 	  …
 	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
 	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)

I can now deserialize by adding the following constructor to the TextMessage class.

	@JsonCreator
	public TextMessage(@JsonProperty("text") String text) {
		this.text = text;
	}

@kazuki-ma
Copy link
Contributor

@kazuki-ma
Copy link
Contributor

Oh sorry, you want to deserialize serialized Message JSON.. Please forget my comment.

Could you tell me why you want to create deserialized Message string?
For push/reply message, using LineMessagingClient is better.

@hamata5656
Copy link
Contributor Author

Reply is done using LineMessagingClient.
However, since I want to manage the message to reply in one table, I want to keep it as a JSON String.

@kazuki-ma
Copy link
Contributor

Thank you your feedback.

I was thinking about your case, but it is not easy to provide Message JSON serialize&deserialize feature, especially with compatibility consideration.

I'll try to support serialize&deserialization in SDK.

But please consider creating your own serialize/deserialize converter, to manage serialization SPEC.

@hamata5656
Copy link
Contributor Author

Thank you for your reply.
I'll create my own serialize / deserialize converter.

@kazuki-ma
Copy link
Contributor

Thank you.

georgexxu added a commit to georgexxu/Line-chatbot-for-COMP3111 that referenced this issue Nov 21, 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

Successfully merging a pull request may close this issue.

3 participants