3

I am doing the Java project with spring.So I am using the Jackson library to convert to get the JSON format.

My java Class will be ,

public class ChatInteraction extends Interaction{

    private int ticketId;
    private String name;

    private String interactionType ;
    private LinkedList<InteractionInfo> interactions;

    public ChatInteraction(Message response) {
        super(response);
        interactions = new LinkedList<InteractionInfo>();
    }


    public int getTicketId() {
        return ticketId;
    }

    public void setTicketId(int ticketId) {
        this.ticketId = ticketId;
        System.out.println("Ticket Id for Interaction : "+this.ticketId);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Name for Interaction : "+this.name);
    }


    public LinkedList<InteractionInfo> getInteractions() {
        return interactions;
    }

    public String getInteractionType() {
        return interactionType;
    }

    public void setInteractionType(String interactionType) {
        this.interactionType = interactionType;
    }


    public void addInteraction(InteractionInfo interaction) {
        this.interactions.add(interaction);
    }


    public void accept(int proxyId,String intxnId,int ticketId){

        RequestAccept reqAccept = RequestAccept.create();
        reqAccept.setProxyClientId(proxyId);
        reqAccept.setInteractionId(intxnId);
        reqAccept.setTicketId(ticketId);

        System.out.println("New Chat RequestAccept Request Object ::: "+reqAccept.toString());

        try{
            if(intxnProtocol.getState() == ChannelState.Opened){

                Message response = intxnProtocol.request(reqAccept);

                System.out.println("New Chat RequestAccept Response ::: "+response.toString());

                if(response != null ){

                    if( response.messageId() == EventAck.ID){
                        System.out.println("Accept new chat  success !");
                        //EventAccepted accept = (EventAccepted)response;
                        //return "New chat Interaction accepted";
                    }else if(response.messageId() == EventError.ID){
                        System.out.println("Accept new chat Failed !");
                        //return "New chat Interaction rejected";
                    }
                }

            }else{
                System.out.println("RequestAccept failure due to Interaction protocol error  !"); 
            }


        }catch(Exception acceptExcpetion){
            acceptExcpetion.printStackTrace();
        }

    }


    public void join(String sessionId, String subject) {

        RequestJoin join = RequestJoin.create();
        join.setMessageText(MessageText.create(""));
        join.setQueueKey("Resources:"); //Add the chat-inbound-key in multimedia of the optional tab values of the softphone application in CME
        join.setSessionId(sessionId);
        join.setVisibility(Visibility.All);
        join.setSubject(subject);
        KeyValueCollection kvc = new KeyValueCollection();
        join.setUserData(kvc);

        System.out.println("Join Request Object ::: "+join.toString());

        try {

            if(basicProtocol != null && basicProtocol.getState() == ChannelState.Opened){
                Message response = basicProtocol.request(join);

                if(response != null){

                    System.out.println("RequestJoin response ::: "+response);

                    if (response.messageId() == EventSessionInfo.ID) {
                        System.out.println("Join Request success !");
                    }else{
                        System.out.println("Join Request Failed !");
                    }
                }
            }else{
                System.out.println("BasicChat protocol Error !");
                //return "BasicChat protocol Error !";
            }
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

    }


}

I need to get only the interactionType and interactions property of this class in the JSON format like ,

 {"interactionType":"invite","interactions" : [{"xx":"XX","yy":"YY"},{"xx":"XX","yy":"YY"}]} 

Note :

  1. I don't need the other properties of this class.

  2. Also there is no SETTER for the interactions property . Instead of that I have the addInteractions() method . Does this affects any behaviour of JSON conversion ?

  3. Also I have some other methods like accept(...) , Join(...).

  4. I am using the jackson-all-1.9.0.jar

3 Answers 3

4

You can annotate the unneeded fields with @JsonIgnore - see Jackson's manual on annotations. That's what it will look like, using your code:

public class ChatInteraction extends Interaction{
    @JsonIgnore
    private int ticketId;
    @JsonIgnore
    private String name;

    private String interactionType ;
    private LinkedList<InteractionInfo> interactions;
Sign up to request clarification or add additional context in comments.

2 Comments

also i dont have the SETTER for interactions property. Does this affects any behaviour of JSON conversion ?
wiki.fasterxml.com/JacksonFAQ#Data_Binding.2C_general - no, since 1.1 it can access member fields without the help of getters/setters.
3

You can use achieve this by using the @JsonIgnoreProperties annotation that can be used on class level.

From JavaDoc:

Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization).

Example:

 // to prevent specified fields from being serialized or deserialized
 // (i.e. not include in JSON output; or being set even if they were included)
 \@JsonIgnoreProperties({ "internalId", "secretKey" })

Example, In your case:

@JsonIgnoreProperties({ "ticketId", "name" })
public class ChatInteraction extends Interaction{
    ....
}

1 Comment

also i dont have the SETTER for interactions property. Does this affects any behaviour of JSON conversion ?
2

Finally I got the solution by others answers in the thread and similar answers in stackoverflow,

  1. I marked the @JsonIgnore in the unwanted field in the sub class and super class suggested by fvu.

  2. I have used the myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); in my objectMapper suggested in other thread like,

    ObjectMapper mapp = new ObjectMapper();
    mapp.setVisibility(JsonMethod.FIELD, Visibility.ANY);
    try {
        json = mapp.writeValueAsString(info);
        info.clear();
        System.out.println("Chat Info in JSON String is :::>  "+json);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.