org.emfjson.jackson.databind.deser.EMapDeserializer.java Source code

Java tutorial

Introduction

Here is the source code for org.emfjson.jackson.databind.deser.EMapDeserializer.java

Source

/*
 * Copyright (c) 2015 Guillaume Hillairet.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Guillaume Hillairet - initial API and implementation
 *
 */
package org.emfjson.jackson.databind.deser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.emfjson.jackson.utils.EObjects;
import org.emfjson.jackson.databind.EMFContext;

import java.io.IOException;
import java.util.Map;

public class EMapDeserializer extends JsonDeserializer<EList<Map.Entry<?, ?>>> {

    @Override
    public EList<Map.Entry<?, ?>> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        return null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public EList<Map.Entry<?, ?>> deserialize(JsonParser jp, DeserializationContext ctxt,
            EList<Map.Entry<?, ?>> intoValue) throws IOException {
        final EReference reference = EMFContext.getReference(ctxt);

        if (jp.getCurrentToken() == JsonToken.START_OBJECT) {

            while (jp.nextToken() != JsonToken.END_OBJECT) {
                final String key = jp.getCurrentName();
                jp.nextToken();

                final Object value;
                if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
                    value = ctxt.readValue(jp, EObject.class);
                } else {
                    value = ctxt.readValue(jp, Object.class);
                }

                // Dynamic objects do not use the EMap interface
                // but store entries in a DynamicEList instead.
                if (intoValue instanceof EMap) {
                    ((EMap) intoValue).put(key, value);
                } else if (reference != null) {
                    intoValue
                            .add((Map.Entry<?, ?>) EObjects.createEntry(key, value, reference.getEReferenceType()));
                }
            }
        }

        return intoValue;
    }

}