• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.fasterxml.jackson.databind.ext;
3 
4 import java.io.IOException;
5 import java.nio.file.Path;
6 
7 import com.fasterxml.jackson.core.JsonGenerator;
8 import com.fasterxml.jackson.core.JsonToken;
9 import com.fasterxml.jackson.core.type.WritableTypeId;
10 
11 import com.fasterxml.jackson.databind.SerializerProvider;
12 import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
13 import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
14 
15 /**
16  * @since 2.8
17  */
18 public class NioPathSerializer extends StdScalarSerializer<Path>
19 {
20     private static final long serialVersionUID = 1;
21 
NioPathSerializer()22     public NioPathSerializer() { super(Path.class); }
23 
24     @Override
serialize(Path value, JsonGenerator gen, SerializerProvider serializers)25     public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
26         // write the Path as a URI, always.
27         gen.writeString(value.toUri().toString());
28     }
29 
30     // [databind#1688]: Not sure this is 100% ok, considering there are legitimately different
31     //  impls... but has to do
32     @Override
serializeWithType(Path value, JsonGenerator g, SerializerProvider provider, TypeSerializer typeSer)33     public void serializeWithType(Path value, JsonGenerator g,
34             SerializerProvider provider, TypeSerializer typeSer) throws IOException
35     {
36         // Better ensure we don't use specific sub-classes:
37         WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
38                 typeSer.typeId(value, Path.class, JsonToken.VALUE_STRING));
39         serialize(value, g, provider);
40         typeSer.writeTypeSuffix(g, typeIdDef);
41     }
42 }
43