Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
ignores one object/array
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneider committed Jan 9, 2012
1 parent bbc9bee commit ed53829
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
@@ -0,0 +1,61 @@
package com.cedarsoft.serialization.jackson;

import com.cedarsoft.version.Version;
import com.cedarsoft.version.VersionException;
import com.cedarsoft.version.VersionRange;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonToken;

import javax.annotation.Nonnull;
import java.io.IOException;

/**
* Simply ignores the object/array
* @author Johannes Schneider (<a href="mailto:js@cedarsoft.com">js@cedarsoft.com</a>)
*/
public class IgnoringSerializer extends AbstractJacksonSerializer<Void> {
public IgnoringSerializer() {
super( "ignoring", VersionRange.single( 0, 0, 0 ) );
}

@Override
public void serialize( @Nonnull JsonGenerator serializeTo, @Nonnull Void object, @Nonnull Version formatVersion ) throws IOException, VersionException, JsonProcessingException {
throw new UnsupportedOperationException();
}

@Nonnull
@Override
public Void deserialize( @Nonnull JsonParser deserializeFrom, @Nonnull Version formatVersion ) throws IOException, VersionException, JsonProcessingException {
JsonToken inToken = deserializeFrom.nextToken();
JsonToken outToken = findOutToken( inToken );

int depth = 1;

while ( depth > 0 ) {
JsonToken next = deserializeFrom.nextToken();
if ( next == inToken ) {
depth++;
}
if ( next == outToken ) {
depth--;
}
}

//noinspection ConstantConditions
return null;
}

@Nonnull
private JsonToken findOutToken( @Nonnull JsonToken inToken ) {
switch ( inToken ) {
case START_OBJECT:
return JsonToken.END_OBJECT;
case START_ARRAY:
return JsonToken.END_ARRAY;
}

throw new IllegalArgumentException( "No end token found for <" + inToken + ">" );
}
}
@@ -0,0 +1,47 @@
package com.cedarsoft.serialization.jackson;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.junit.*;
import org.junit.experimental.theories.*;
import org.junit.runner.*;

import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;

import static org.fest.assertions.Assertions.assertThat;

/**
* @author Johannes Schneider (<a href="mailto:js@cedarsoft.com">js@cedarsoft.com</a>)
*/
@RunWith( Theories.class )
public class IgnoringSerializerTest {

private IgnoringSerializer serializer;

@Before
public void setUp() throws Exception {
serializer = new IgnoringSerializer();
}

@Theory
public void testIt( @Nonnull String json ) throws Exception {
JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
JsonParser parser = jsonFactory.createJsonParser( new ByteArrayInputStream( json.getBytes() ) );

Void result = serializer.deserialize( parser );
assertThat( result ).isNull();
}

@DataPoints
public static String[] testIt() throws Exception {
return new String[]{
"{}",
"[]",
"[1,2,3,4]",
"{\"id\":123}",
"{\"id\":[123]}",
"{\"id\":{\"value\":123}}"
};
}
}

0 comments on commit ed53829

Please sign in to comment.