This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbc9bee
commit ed53829
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
jackson/src/main/java/com/cedarsoft/serialization/jackson/IgnoringSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + ">" ); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
jackson/src/test/java/com/cedarsoft/serialization/jackson/IgnoringSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}}" | ||
}; | ||
} | ||
} |