Package org.jboss.errai.marshalling.client.api.json

Examples of org.jboss.errai.marshalling.client.api.json.EJObject


  public Map<String, Object> demarshall(final EJValue o, final MarshallingSession ctx) {
    return doDemarshall(new HashMap<String, Object>(), o, ctx);
  }

  protected Map<String, Object> doDemarshall(final Map<String, Object> impl, final EJValue o, final MarshallingSession ctx) {
    final EJObject jsonObject = o.isObject();
    if (jsonObject == null)
      return null;

    for (final String key : jsonObject.keySet()) {
      final EJValue v = jsonObject.get(key);
      if (!v.isNull()) {
        final String type = ctx.determineTypeFor(null, v);

        if (type == null) {
          impl.put(key, v.toString());
View Full Code Here


  }

  protected Map<String, Object> doDemarshall(final Map<String, Object> impl,
                                             final EJValue o,
                                             final MarshallingSession ctx) {
    final EJObject jsonObject = o.isObject();

    for (final String key : jsonObject.keySet()) {
      if (MessageParts.SessionID.name().equals(key))
        continue;
      final EJValue v = jsonObject.get(key);
      if (!v.isNull()) {
        final Marshaller<Object> marshallerInstance = ctx.getMarshallerInstance(ctx.determineTypeFor(null, v));
        impl.put(key, marshallerInstance.demarshall(v, ctx));
      }
      else {
View Full Code Here

    return delegate.getEmptyArray();
  }
 
  @Override
  public T doNotNullDemarshall(final EJValue o, final MarshallingSession ctx) {
    final EJObject obj = o.isObject();
   
    if (obj != null) {
      final String objId = obj.get(SerializationParts.OBJECT_ID).isString().stringValue();
      if (ctx.hasObject(objId)) {
        // noinspection unchecked
        return (T) ctx.getObject(Object.class, objId);
      }

      EJValue val = obj.get(SerializationParts.QUALIFIED_VALUE);
      if (val.isNull()) {
        val = o;
      }
      return ctx.recordObject(objId, delegate.demarshall(val, ctx));
    }
View Full Code Here

    if (o.isNull()) {
      return null;
    }

    if (o.isObject() != null) {
      final EJObject jsObject = o.isObject();
      final EJValue ejEncType = jsObject.get(SerializationParts.ENCODED_TYPE);
      String encodedType = null;
      if (!ejEncType.isNull() && ejEncType.isString() != null) {
        encodedType = ejEncType.isString().stringValue();
      }

      if (encodedType == null) {
        if (targetType == null) {
          return MapMarshaller.INSTANCE.demarshall(o, ctx);
        }
        else if (ctx.getMarshallerInstance(targetType.getName()) != null) {
          return ctx.getMarshallerInstance(targetType.getName()).demarshall(o, ctx);
        }
      }

      if (jsObject.containsKey(SerializationParts.NUMERIC_VALUE)) {
        return NumbersUtils.getNumber(encodedType, jsObject.get(SerializationParts.NUMERIC_VALUE));
      }

      if (ctx.getMarshallerInstance(encodedType) == null) {
        throw new RuntimeException("marshalled type is unknown to the marshalling framework: " + encodedType);
      }
View Full Code Here

  @Override
  public String determineTypeFor(final String formatType, final Object o) {
    final EJValue jsonValue = (EJValue) o;

    if (jsonValue.isObject() != null) {
      final EJObject jsonObject = jsonValue.isObject();
      if (jsonObject.containsKey(SerializationParts.ENCODED_TYPE)) {
        return jsonObject.get(SerializationParts.ENCODED_TYPE).isString().stringValue();
      }
      else {
        return Map.class.getName();
      }
    }
View Full Code Here

  @SuppressWarnings({"ConstantConditions", "unchecked"})
  @Override
  public Object demarshall(final EJValue o, final MarshallingSession ctx) {
    try {
      if (o.isObject() != null) {
        final EJObject oMap = o.isObject();
        final Object newInstance;

        if (MarshallUtil.isEncodedObject(oMap)) {
          if (MarshallUtil.isEncodedNumeric(oMap)) {
            return NumbersUtils.getEncodedNumber(oMap);
          }

          final String objID = oMap.get(SerializationParts.OBJECT_ID).isString().stringValue();

          if (ctx.hasObject(objID)) {
            newInstance = ctx.getObject(Object.class, objID);

            /**
             * If this only contains 2 fields, it is only a graph reference.
             */
            if (oMap.size() == 2) {
              return newInstance;
            }
          }
          else {
            /**
             * Check to see if this object is instantiate only... meaning it has no fields to marshall.
             */
            if (oMap.containsKey(SerializationParts.INSTANTIATE_ONLY)) {
              newInstance = getTypeHandled().newInstance();
              ctx.recordObject(objID, newInstance);
              return newInstance;
            }

            final InstantiationMapping cMapping = definition.getInstantiationMapping();
            final Object[] parms = new Object[cMapping.getMappings().length];
            final Class[] targetTypes = cMapping.getSignature();

            int i = 0;
            for (final Mapping mapping : cMapping.getMappings()) {
              final Marshaller<Object> marshaller = ctx.getMarshallerInstance(mapping.getType().getFullyQualifiedName());
              //noinspection unchecked
              parms[i] = DataConversion.convert(
                      marshaller.demarshall(oMap.get(mapping.getKey()), ctx), targetTypes[i++]);
            }

            if (cMapping instanceof ConstructorMapping) {
              final Constructor constructor = ((ConstructorMapping) cMapping).getMember().asConstructor();
              constructor.setAccessible(true);
              newInstance = constructor.newInstance(parms);
            }
            else {
              newInstance = ((FactoryMapping) cMapping).getMember().asMethod().invoke(null, parms);
            }

            ctx.recordObject(objID, newInstance);
          }

          for (final MemberMapping mapping : definition.getWritableMemberMappings()) {
            final EJValue o1 = oMap.get(mapping.getKey());

            if (!o1.isNull()) {
              final Marshaller<Object> marshaller
                      = ctx.getMarshallerInstance(mapping.getType().getFullyQualifiedName());

              if (mapping.getBindingMember() instanceof MetaField) {
                final MetaField f = (MetaField) mapping.getBindingMember();

                setProperty(newInstance, f.asField(),
                        marshaller.demarshall(o1, ctx));
              }
              else {
                final Method m = ((MetaMethod) mapping.getBindingMember()).asMethod();

                m.invoke(newInstance, DataConversion.convert(
                        marshaller.demarshall(o1, ctx),
                        m.getParameterTypes()[0]));
              }
            }
          }

          return newInstance;
        }
        else if (oMap.containsKey(SerializationParts.ENUM_STRING_VALUE)) {
          return Enum.valueOf(getClassReference(oMap),
                  oMap.get(SerializationParts.ENUM_STRING_VALUE).isString().stringValue());
        }
        else {
          throw new RuntimeException("bad payload");
        }
      }
View Full Code Here

  @SuppressWarnings({"ConstantConditions", "unchecked"})
  @Override
  public Object demarshall(final EJValue o, final MarshallingSession ctx) {
    try {
      if (o.isObject() != null) {
        final EJObject oMap = o.isObject();
        final Object newInstance;

        if (MarshallUtil.isEncodedObject(oMap)) {
          if (MarshallUtil.isEncodedNumeric(oMap)) {
            return NumbersUtils.getEncodedNumber(oMap);
          }

          final String objID = oMap.get(SerializationParts.OBJECT_ID).isString().stringValue();

          if (ctx.hasObject(objID)) {
            newInstance = ctx.getObject(Object.class, objID);

            /**
             * If this only contains 2 fields, it is only a graph reference.
             */
            if (oMap.size() == 2) {
              return newInstance;
            }
          }
          else {
            /**
             * Check to see if this object is instantiate only... meaning it has no fields to marshall.
             */
            if (oMap.containsKey(SerializationParts.INSTANTIATE_ONLY)) {
              newInstance = getTypeHandled().newInstance();
              ctx.recordObject(objID, newInstance);
              return newInstance;
            }

            final InstantiationMapping cMapping = definition.getInstantiationMapping();
            final Object[] parms = new Object[cMapping.getMappings().length];
            final Class[] targetTypes = cMapping.getSignature();

            int i = 0;
            for (final Mapping mapping : cMapping.getMappings()) {
              final Marshaller<Object> marshaller = ctx.getMarshallerInstance(mapping.getType().getFullyQualifiedName());
              //noinspection unchecked
              parms[i] = DataConversion.convert(
                      marshaller.demarshall(oMap.get(mapping.getKey()), ctx), targetTypes[i++]);
            }

            if (cMapping instanceof ConstructorMapping) {
              final Constructor constructor = ((ConstructorMapping) cMapping).getMember().asConstructor();
              constructor.setAccessible(true);
              newInstance = constructor.newInstance(parms);
            }
            else {
              newInstance = ((FactoryMapping) cMapping).getMember().asMethod().invoke(null, parms);
            }

            ctx.recordObject(objID, newInstance);
          }

          for (final MemberMapping mapping : definition.getWritableMemberMappings()) {
            final EJValue o1 = oMap.get(mapping.getKey());

            if (!o1.isNull()) {
              final Marshaller<Object> marshaller
                      = ctx.getMarshallerInstance(mapping.getType().getFullyQualifiedName());

              if (mapping.getBindingMember() instanceof MetaField) {
                final MetaField f = (MetaField) mapping.getBindingMember();

                setProperty(newInstance, f.asField(),
                        marshaller.demarshall(o1, ctx));
              }
              else {
                final Method m = ((MetaMethod) mapping.getBindingMember()).asMethod();

                m.invoke(newInstance, DataConversion.convert(
                        marshaller.demarshall(o1, ctx),
                        m.getParameterTypes()[0]));
              }
            }
          }

          return newInstance;
        }
        else if (oMap.containsKey(SerializationParts.ENUM_STRING_VALUE)) {
          return Enum.valueOf(getClassReference(oMap),
                  oMap.get(SerializationParts.ENUM_STRING_VALUE).isString().stringValue());
        }
        else {
          throw new RuntimeException("bad payload");
        }
      }
View Full Code Here

    }

    @Override
    public String determineTypeFor(final String formatType, final Object o) {
      if (((EJValue) o).isObject() != null) {
        final EJObject jsonObject = ((EJValue) o).isObject();
        if (jsonObject.containsKey(SerializationParts.ENCODED_TYPE)) {
          return jsonObject.get(SerializationParts.ENCODED_TYPE).isString().stringValue();
        }
        else {
          return Map.class.getName();
        }
      }
View Full Code Here

    if (o.isNull()) {
      return null;
    }

    if (o.isObject() != null) {
      final EJObject jsObject = o.isObject();
      final EJValue ejEncType = jsObject.get(SerializationParts.ENCODED_TYPE);
      String encodedType = null;
      if (!ejEncType.isNull() && ejEncType.isString() != null) {
        encodedType = ejEncType.isString().stringValue();
      }

      if (encodedType == null) {
        if (targetType == null) {
          if (jsObject.containsKey(SerializationParts.QUALIFIED_VALUE)) {
            // the object we're decoding is a wrapper with ^ObjectID and ^Value, but no type information.
            // just bypass this layer and return the "meat"
            return demarshall(Object.class, jsObject.get(SerializationParts.QUALIFIED_VALUE), ctx);
          }
          // without a ^Value property, this must be a Map
          return MapMarshaller.INSTANCE.demarshall(o, ctx);
        }
        else if (ctx.getMarshallerInstance(targetType.getName()) != null) {
          return ctx.getMarshallerInstance(targetType.getName()).demarshall(o, ctx);
        }
      }

      if (jsObject.containsKey(SerializationParts.NUMERIC_VALUE)) {
        return NumbersUtils.getNumber(encodedType, jsObject.get(SerializationParts.NUMERIC_VALUE));
      }

      if (ctx.getMarshallerInstance(encodedType) == null) {
        throw new RuntimeException("marshalled type is unknown to the marshalling framework: " + encodedType);
      }
View Full Code Here

  public Map<String, Object> demarshall(final EJValue o, final MarshallingSession ctx) {
    return doDemarshall(new HashMap<String, Object>(), o, ctx);
  }

  protected Map<String, Object> doDemarshall(final Map<String, Object> impl, final EJValue o, final MarshallingSession ctx) {
    final EJObject jsonObject = o.isObject();
    if (jsonObject == null)
      return null;

    for (final String key : jsonObject.keySet()) {
      final EJValue v = jsonObject.get(key);
      if (!v.isNull()) {
        final String type = ctx.determineTypeFor(null, v);

        if (type == null) {
          impl.put(key, v.toString());
View Full Code Here

TOP

Related Classes of org.jboss.errai.marshalling.client.api.json.EJObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.