Java tutorial
/******************************************************************************* * Copyright (c) 2013 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.eclipselabs.emfjson.gwt.map; import static org.eclipselabs.emfjson.gwt.common.ModelUtil.getDynamicMapEntryFeature; import static org.eclipselabs.emfjson.gwt.common.ModelUtil.getEAttribute; import java.util.Collection; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EAttribute; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.ecore.util.EcoreUtil; import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONValue; class EAtttributeDeserializer { private MapDeserializer mapDeserializer; EAtttributeDeserializer() { this.mapDeserializer = new MapDeserializer(); } void deSerialize(EObject eObject, JSONValue node) { final EClass eClass = eObject.eClass(); final JSONObject root = node.isObject(); if (root == null) return; // Iterates over all key values of the JSON Object, // if the value is not an object then // if the key corresponds to an EAttribute, fill it // if not and the EClass contains a MapEntry, fill it with the key, value. for (String key : root.keySet()) { JSONValue value = root.get(key); if (value.isObject() != null) // not an attribute continue; EAttribute attribute = getEAttribute(eClass, key); if (isCandidate(attribute)) { if (value.isArray() != null) { JSONArray array = value.isArray(); for (int i = 0; i < array.size(); i++) { deSerializeValue(eObject, attribute, array.get(i)); } } else { deSerializeValue(eObject, attribute, value); } } else { EStructuralFeature eFeature = getDynamicMapEntryFeature(eClass); if (eFeature != null) { @SuppressWarnings("unchecked") EList<EObject> values = (EList<EObject>) eObject.eGet(eFeature); values.add(mapDeserializer.deSerializeEntry(key, value)); } } } } void deSerializeValue(EObject eObject, EAttribute attribute, JSONValue value) { final String stringValue; if (value.isString() != null) stringValue = value.isString().stringValue(); else if (value.isBoolean() != null) stringValue = Boolean.toString(value.isBoolean().booleanValue()); else if (value.isNumber() != null) stringValue = value.toString(); else stringValue = ""; if (stringValue != null && !stringValue.trim().isEmpty()) { Object newValue; if (attribute.getEAttributeType().getInstanceClass().isEnum()) { newValue = EcoreUtil.createFromString(attribute.getEAttributeType(), stringValue.toUpperCase()); } else { newValue = EcoreUtil.createFromString(attribute.getEAttributeType(), stringValue); } if (!attribute.isMany()) { eObject.eSet(attribute, newValue); } else { @SuppressWarnings("unchecked") Collection<Object> values = (Collection<Object>) eObject.eGet(attribute); values.add(newValue); } } } boolean isCandidate(EAttribute attribute) { return attribute != null && !attribute.isTransient() && !attribute.isDerived(); } }