Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Add default transformer for File/Charset object.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongHwan committed Sep 24, 2019
1 parent aea7667 commit b652569
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.realtimetech.kson;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -71,7 +73,31 @@ public KsonContext(int stackSize, int stringBufferSize) {
this.primaryObjects = new HashMap<Class<?>, HashMap<Object, Object>>();

this.cachedFields = new HashMap<Class<?>, Field[]>();

this.registerTransformer(Charset.class, new Transformer<Charset>() {
@Override
public Object serialize(KsonContext ksonContext, Charset value) {
return value.toString();
}

@Override
public Charset deserialize(KsonContext ksonContext, Class<?> object, Object value) {
return Charset.forName(value.toString());
}
});

this.registerTransformer(File.class, new Transformer<File>() {
@Override
public Object serialize(KsonContext ksonContext, File value) {
return value.getAbsolutePath();
}

@Override
public File deserialize(KsonContext ksonContext, Class<?> object, Object value) {
return new File(value.toString());
}
});

this.registeredTransformers.put(Date.class, new Transformer<Date>() {
@Override
public Object serialize(KsonContext ksonContext, Date value) {
Expand Down

0 comments on commit b652569

Please sign in to comment.