TelegramBots/src/main/java/org/telegram/telegrambots/api/objects/Contact.java

69 lines
2.4 KiB
Java
Raw Normal View History

2016-01-14 23:09:19 +01:00
package org.telegram.telegrambots.api.objects;
2016-01-14 01:14:53 +01:00
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
2016-01-14 23:09:19 +01:00
import org.telegram.telegrambots.api.interfaces.IBotApiObject;
2016-01-14 01:14:53 +01:00
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object represents a phone contact.
* @date 20 of June of 2015
*/
2016-01-14 23:09:19 +01:00
public class Contact implements IBotApiObject {
2016-01-14 01:14:53 +01:00
public static final String PHONENUMBER_FIELD = "phone_number";
@JsonProperty(PHONENUMBER_FIELD)
private String phoneNumber; ///< Contact's phone number
public static final String FIRSTNAME_FIELD = "first_name";
@JsonProperty(FIRSTNAME_FIELD)
private String firstName; ///< Contact's first name
public static final String LASTNAME_FIELD = "last_name";
@JsonProperty(LASTNAME_FIELD)
private String lastName; ///< Optional. Contact's last name
public static final String USERID_FIELD = "user_id";
@JsonProperty(USERID_FIELD)
private Integer userID; ///< Optional. Contact's user identifier in Telegram
public Contact() {
super();
}
public Contact(JSONObject jsonObject) {
super();
this.phoneNumber = jsonObject.getString(PHONENUMBER_FIELD);
this.firstName = jsonObject.getString(FIRSTNAME_FIELD);
if (jsonObject.has(LASTNAME_FIELD)) {
this.lastName = jsonObject.getString(LASTNAME_FIELD);
}
if (jsonObject.has(USERID_FIELD)) {
this.userID = jsonObject.getInt(USERID_FIELD);
}
}
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeStringField(PHONENUMBER_FIELD, phoneNumber);
gen.writeStringField(FIRSTNAME_FIELD, firstName);
if (lastName != null) {
gen.writeStringField(LASTNAME_FIELD, lastName);
}
if (userID != null) {
gen.writeNumberField(USERID_FIELD, userID);
}
gen.writeEndObject();
gen.flush();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
}