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
Showing
4 changed files
with
107 additions
and
51 deletions.
There are no files selected for viewing
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
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
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
73 changes: 73 additions & 0 deletions
73
src/test/java/com/alibaba/json/test/codec/Jackson2AfterBurnCodec.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,73 @@ | |||
package com.alibaba.json.test.codec; | |||
|
|||
import java.util.Collection; | |||
|
|||
import com.fasterxml.jackson.core.type.TypeReference; | |||
import com.fasterxml.jackson.databind.ObjectMapper; | |||
import com.fasterxml.jackson.databind.node.ObjectNode; | |||
import com.fasterxml.jackson.module.afterburner.AfterburnerModule; | |||
|
|||
|
|||
public class Jackson2AfterBurnCodec implements Codec { | |||
|
|||
private ObjectMapper mapper = new ObjectMapper(); | |||
|
|||
public Jackson2AfterBurnCodec() { | |||
mapper.registerModule(new AfterburnerModule()); | |||
} | |||
|
|||
public String getName() { | |||
return "jackson2AfterBurn"; | |||
} | |||
|
|||
public final <T> T decodeObject(String text, Class<T> clazz) { | |||
try { | |||
return mapper.readValue(text, clazz); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e.getMessage(), e); | |||
} | |||
} | |||
|
|||
|
|||
public <T> T decodeObject(byte[] input, Class<T> clazz) throws Exception { | |||
try { | |||
return mapper.readValue(input, clazz); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e.getMessage(), e); | |||
} | |||
} | |||
|
|||
public <T> Collection<T> decodeArray(String text, Class<T> clazz) throws Exception { | |||
try { | |||
return (Collection<T>) mapper.readValue(text, new TypeReference<T>() { | |||
}); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e.getMessage(), e); | |||
} | |||
} | |||
|
|||
public final Object decodeObject(String text) { | |||
try { | |||
return (ObjectNode) mapper.readTree(text); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e.getMessage(), e); | |||
} | |||
} | |||
|
|||
public Object decode(String text) { | |||
try { | |||
return mapper.readTree(text); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e.getMessage(), e); | |||
} | |||
} | |||
|
|||
public String encode(Object object) throws Exception { | |||
return mapper.writeValueAsString(object); | |||
} | |||
|
|||
@Override | |||
public byte[] encodeToBytes(Object object) throws Exception { | |||
return mapper.writeValueAsBytes(object); | |||
} | |||
} |