Package org.qi4j.api.json

Examples of org.qi4j.api.json.JSONDeserializer


        throws Exception
    {
        ValueType collectedType = new ValueType( BigInteger.class );
        CollectionType collectionType = new CollectionType( Collection.class, collectedType );
        Object json = new JSONArray( bigIntegerJsonWithQuotes() );
        List<BigDecimal> result = (List<BigDecimal>) new JSONDeserializer( null ).deserialize( json, collectionType );
        assertEquals( bigIntegerCollection(), result );
    }
View Full Code Here


        throws Exception
    {
        ValueType collectedType = new ValueType( BigDecimal.class );
        CollectionType collectionType = new CollectionType( Collection.class, collectedType );
        Object json = new JSONArray( bigDecimalJsonWithQuotes() );
        List<BigDecimal> result = (List<BigDecimal>) new JSONDeserializer( null ).deserialize( json, collectionType );
        assertEquals( bigDecimalCollection(), result );
    }
View Full Code Here

    @Test
    public void givenLocalDateTypeWhenConvertingFromJsonExpectValidLocalDate()
        throws Exception
    {
        Object value = new JSONDeserializer( null ).deserialize( "2020-03-04", underTest );
        assertEquals( new LocalDate("2020-03-04"), value);
    }
View Full Code Here

    @Test
    public void givenLocalDateTypeWhenConvertingFromJsonExpectValidLocalDate()
        throws Exception
    {
        Object value = new JSONDeserializer( null ).deserialize( "2020-03-04T12:23:09", underTest );
        assertEquals( new LocalDateTime("2020-03-04T12:23:09", DateTimeZone.UTC ), value);
    }
View Full Code Here

    @Test
    public void givenLocalDateTypeWhenConvertingFromJsonExpectValidLocalDate()
        throws Exception
    {
        Object value = new JSONDeserializer( null ).deserialize( "2020-03-04T12:23:33Z", underTest );
        assertEquals( new DateTime("2020-03-04T12:23:33Z", DateTimeZone.UTC ), value);
    }
View Full Code Here

        {
            JSONObjectSerializer serializer = new JSONObjectSerializer();
            serializer.serialize( prototype, model.model().valueType() );
            Object object = serializer.getRoot();

            JSONDeserializer deserializer = new JSONDeserializer( model.module() );
            value = deserializer.deserialize( object, model.model().valueType() );
        } catch( JSONException e )
        {
            throw new IllegalStateException( "Could not JSON-copy Value", e );
        }
View Full Code Here

            throw new NoSuchValueException( mixinType.getName(), name() );
        }

        try
        {
            return (T) new JSONDeserializer( model.module() ).deserialize( new JSONTokener( jsonValue ).nextValue(), model.model().valueType() );
        } catch( JSONException e )
        {
            throw new ConstructionException( "Could not create value from JSON", e );
        }
    }
View Full Code Here

                PropertyDescriptor descriptor = entityDescriptor.state().getPropertyByQualifiedName( stateName );

                if (descriptor == null)
                    return null;

                JSONDeserializer deserializer = new JSONDeserializer( module );

                return deserializer.deserialize( json, descriptor.valueType() );
            }
        }
        catch( JSONException e )
        {
            throw new EntityStoreException( e );
View Full Code Here

        throws EntityStoreException
    {
        try
        {
            Module module = unitOfWork.module();
            JSONDeserializer deserializer = new JSONDeserializer( module );
            JSONObject jsonObject = new JSONObject( new JSONTokener( entityState ) );
            EntityStatus status = EntityStatus.LOADED;

            String version = jsonObject.getString( "version" );
            long modified = jsonObject.getLong( "modified" );
            String identity = jsonObject.getString( "identity" );

            // Check if version is correct
            String currentAppVersion = jsonObject.optString( MapEntityStore.JSONKeys.application_version.name(),
                                                             "0.0" );
            if( !currentAppVersion.equals( application.version() ) )
            {
                if( migration != null )
                {
                    migration.migrate( jsonObject, application.version(), this );
                }
                else
                {
                    // Do nothing - set version to be correct
                    jsonObject.put( MapEntityStore.JSONKeys.application_version.name(), application.version() );
                }

                LoggerFactory.getLogger( MapEntityStoreMixin.class )
                    .debug(
                        "Updated version nr on " + identity + " from " + currentAppVersion + " to " + application.version() );

                // State changed
                status = EntityStatus.UPDATED;
            }

            String type = jsonObject.getString( "type" );

            EntityDescriptor entityDescriptor = module.entityDescriptor( type );
            if( entityDescriptor == null )
            {
                throw new EntityTypeNotFoundException( type );
            }

            Map<QualifiedName, Object> properties = new HashMap<QualifiedName, Object>();
            JSONObject props = jsonObject.getJSONObject( "properties" );
            for( PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties() )
            {
                Object jsonValue;
                try
                {
                    jsonValue = props.get( propertyDescriptor.qualifiedName().name() );
                }
                catch( JSONException e )
                {
                    // Value not found, default it
                    Object initialValue = propertyDescriptor.initialValue( module );
                    properties.put( propertyDescriptor.qualifiedName(), initialValue );
                    status = EntityStatus.UPDATED;
                    continue;
                }
                if( jsonValue == JSONObject.NULL )
                {
                    properties.put( propertyDescriptor.qualifiedName(), null );
                }
                else
                {
                    Object value = deserializer.deserialize( jsonValue, propertyDescriptor.valueType() );
                    properties.put( propertyDescriptor.qualifiedName(), value );
                }
            }

            Map<QualifiedName, EntityReference> associations = new HashMap<QualifiedName, EntityReference>();
View Full Code Here

TOP

Related Classes of org.qi4j.api.json.JSONDeserializer

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.