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
6 changed files
with
114 additions
and
7 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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/alibaba/fastjson/parser/deserializer/PathDeserializer.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,32 @@ | |||
package com.alibaba.fastjson.parser.deserializer; | |||
|
|||
import java.lang.reflect.Type; | |||
import java.nio.file.Paths; | |||
|
|||
import com.alibaba.fastjson.parser.DefaultJSONParser; | |||
import com.alibaba.fastjson.parser.JSONLexer; | |||
import com.alibaba.fastjson.parser.JSONToken; | |||
|
|||
public class PathDeserializer implements ObjectDeserializer { | |||
|
|||
@SuppressWarnings("unchecked") | |||
@Override | |||
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { | |||
JSONLexer lexer = parser.lexer; | |||
if (lexer.token() == JSONToken.NULL) { | |||
lexer.nextToken(); | |||
return null; | |||
} | |||
|
|||
String path = lexer.stringVal(); | |||
lexer.nextToken(JSONToken.COMMA); | |||
|
|||
return (T) Paths.get(path); | |||
} | |||
|
|||
@Override | |||
public int getFastMatchToken() { | |||
return JSONToken.LITERAL_STRING; | |||
} | |||
|
|||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/alibaba/fastjson/serializer/ToStringSerializer.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,24 @@ | |||
package com.alibaba.fastjson.serializer; | |||
|
|||
import java.io.IOException; | |||
import java.lang.reflect.Type; | |||
|
|||
public class ToStringSerializer implements ObjectSerializer { | |||
|
|||
public static final ToStringSerializer instance = new ToStringSerializer(); | |||
|
|||
@Override | |||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, | |||
int features) throws IOException { | |||
SerializeWriter out = serializer.out; | |||
|
|||
if (object == null) { | |||
out.writeNull(); | |||
return; | |||
} | |||
|
|||
String strVal = object.toString(); | |||
out.writeString(strVal); | |||
} | |||
|
|||
} |
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,28 @@ | |||
package com.alibaba.json.bvt.jdk7; | |||
|
|||
import java.nio.file.Path; | |||
import java.nio.file.Paths; | |||
|
|||
import org.junit.Assert; | |||
|
|||
import com.alibaba.fastjson.JSON; | |||
|
|||
import junit.framework.TestCase; | |||
|
|||
public class PathTest extends TestCase { | |||
public void test_for_path() throws Exception { | |||
Model model = new Model(); | |||
model.path = Paths.get("/root/fastjson"); | |||
|
|||
String text = JSON.toJSONString(model); | |||
|
|||
Assert.assertEquals("{\"path\":\"/root/fastjson\"}", text); | |||
|
|||
Model model2 = JSON.parseObject(text, Model.class); | |||
Assert.assertEquals(model.path.toString(), model2.path.toString()); | |||
} | |||
|
|||
public static class Model { | |||
public Path path; | |||
} | |||
} |