pengrad / java-telegram-bot-api

Telegram Bot API for Java
https://core.telegram.org/bots
Apache License 2.0
1.81k stars 374 forks source link

need help deserialize responses #181

Closed Jitterer closed 5 years ago

Jitterer commented 5 years ago

Hi,

first thx for this project and your library! For information: I'm completely java beginner what is the reason for my question I think ;) Hopefully you will answer and help me out.

I have trouble to get the serialized list deserialized as an ArrayList. I'm trying everything for hours now. Could you help me out to get the serialized list deserialized with an completely accessible ArrayList? Thx in advance!

BR

pengrad commented 5 years ago

Hi, 1) could you show an example? You want to have java object with ArrayList from json array "cars":[ "Ford", "BMW", "Fiat" ] ?

2) Is it really needed when using this library? Everything should be already deserialized.

Jitterer commented 5 years ago

Hi Pengrad,

thx for your very quick response.

  1. yes I likely need an ArrayList as java object.
  2. yes I need it for my application I'm working with. Currently this is the response (example): GetChatResponse{result=Chat{id=-xxx, type=group, first_name='null', last_name='null', username='null', title='xxx', all_members_are_administrators=true, photo=null, description='null', invite_link='null', pinned_message=null, permissions=ChatPermissions{can_send_messages=true, can_send_media_messages=true, can_send_polls=true, can_send_other_messages=true, can_add_web_page_previews=true, can_change_info=true, can_invite_users=true, can_pin_messages=true}, sticker_set_name='null', can_set_sticker_set=null}}

this seems (for my understanding) to be serialized. something with I cant work with. My current workaround is like creating a map and inserting with the available methods like m.put("rc", updates.errorCode()) etc. ne key=value pairs. But of course I just dont want to do it with every key value pair. So I'm searching to convert the response to an arraylist

pengrad commented 5 years ago

Well, it's kind of serialized, but actually not :) It's just a string version GetChatResponse object which you get via printing or calling toString() method

GetChatResponse getChatResponse = bot.execute(new GetChat(chatId));
System.out.println(getChatResponse);

And you can access any field of this object:

Chat chat = getChatResponse.chat();
Long id = chat.id();
String firstName = chat.firstName();
ChatPermissions permissions = chat.permissions();

And if some field represents json's array - it will be java array.

You can check more examples in Test class https://github.com/pengrad/java-telegram-bot-api/blob/master/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java#L366

Jitterer commented 5 years ago

yes, I already found out that I can access the fields. But what i do NOT want to do is (what I currently doing), to create a new arraylist/map structure where I need to write every available method into a new key value map (like in my post before with map.put("rc", updates.errorCode()).

So I'm searching a comfortable way to directly convert the response as an arraylist (or even json or any way to work with but not as a string which I cant parse).

I'm very thankful for your help!

pengrad commented 5 years ago

You can convert Java object back to json with Gson:

BaseResponse response = bot.execute(new GetChat(groupId));
String json = new Gson().toJson(response);
System.out.println(json);
Jitterer commented 5 years ago

Thx a lot! I never thought this way. Does work like a charm! I appreciate it!