001/*
002 *   Copyright 2021 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.insight;
017
018import com.fasterxml.jackson.core.JsonParser;
019import com.fasterxml.jackson.databind.DeserializationContext;
020import com.fasterxml.jackson.databind.JsonNode;
021import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
022import com.fasterxml.jackson.databind.node.JsonNodeType;
023import java.io.IOException;
024
025public class RoamingDeseriazlizer extends StdDeserializer<RoamingDetails> {
026    public RoamingDeseriazlizer(){
027        this(null);
028    }
029
030    public RoamingDeseriazlizer(Class<?> vc){
031        super(vc);
032    }
033
034    @Override
035    public RoamingDetails deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
036        JsonNode node = p.getCodec().readTree(p);
037        RoamingDetails.RoamingStatus status = RoamingDetails.RoamingStatus.UNKNOWN;
038        String roamingCountryCode = null;
039        String roamingNetworkCode = null;
040        String roamingNetworkName = null;
041        if (node.getNodeType() == JsonNodeType.STRING) {
042            status = RoamingDetails.RoamingStatus.fromString(node.asText());
043        }
044        else if (node.getNodeType() == JsonNodeType.OBJECT) {
045            status = RoamingDetails.RoamingStatus.fromString(node.get("status").asText());
046            if (!(node.get("roaming_country_code") == null)) {
047                roamingCountryCode = node.get("roaming_country_code").asText();
048            }
049            if (!(node.get("roaming_network_code") == null)) {
050                roamingNetworkCode = node.get("roaming_network_code").asText();
051            }
052            if (!(node.get("roaming_network_name") == null)) {
053                roamingNetworkName = node.get("roaming_network_name").asText();
054            }
055        }
056        return new RoamingDetails(status, roamingCountryCode, roamingNetworkCode, roamingNetworkName);
057    }
058}