Skip to content

Commit

Permalink
Add support for serializing and deserializing java.nio.path
Browse files Browse the repository at this point in the history
  • Loading branch information
Katie Richter authored and Katie-Richter committed Jul 2, 2020
1 parent 3ad49b2 commit 7980412
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -117,6 +118,8 @@ private static Map<Class<?>, SerializerProviderWrapper> initSerializers() {
new SerializerProviderWrapper(OptionalIntTypeSerializer::new, OptionalIntTypeDeserializer::new));
serializers.put(OptionalLong.class,
new SerializerProviderWrapper(OptionalLongTypeSerializer::new, OptionalLongTypeDeserializer::new));
serializers.put(Path.class,
new SerializerProviderWrapper(PathTypeSerializer::new, PathTypeDeserializer::new));
serializers.put(Short.class, new SerializerProviderWrapper(ShortTypeSerializer::new, ShortTypeDeserializer::new));
serializers.put(Short.TYPE, new SerializerProviderWrapper(ShortTypeSerializer::new, ShortTypeDeserializer::new));
serializers.put(String.class, new SerializerProviderWrapper(StringTypeSerializer::new, StringTypeDeserializer::new));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2020 IBM and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal.serializer;

import java.lang.reflect.Type;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.yasson.internal.Unmarshaller;
import org.eclipse.yasson.internal.model.customization.Customization;

public class PathTypeDeserializer extends AbstractValueTypeDeserializer<Path> {
public PathTypeDeserializer(Customization customization) {
super(Path.class, customization);
}

@Override
protected Path deserialize(String jsonValue, Unmarshaller unmarshaller, Type rtType) {
return Paths.get(jsonValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2020 IBM and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal.serializer;

import java.nio.file.Path;

import jakarta.json.stream.JsonGenerator;

import org.eclipse.yasson.internal.Marshaller;
import org.eclipse.yasson.internal.model.customization.Customization;

public class PathTypeSerializer extends AbstractValueTypeSerializer<Path> {
public PathTypeSerializer(Customization customization) {
super(customization);
}

@Override
protected void serialize(Path obj, JsonGenerator generator, Marshaller marshaller) {
generator.write(obj.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.yasson.defaultmapping.typeConvertors;

import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.eclipse.yasson.Jsonbs.*;

Expand All @@ -22,8 +23,13 @@
import org.eclipse.yasson.internal.JsonBindingBuilder;

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.config.BinaryDataStrategy;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.UUID;

Expand Down Expand Up @@ -108,12 +114,31 @@ public void testByteArrayWithStrictJsonAndBinaryStrategy() {
result = jsonb.fromJson(base64UrlEncodedJson, ByteArrayWrapper.class);
assertArrayEquals(array, result.array);
}

@Test
public void testUUID() {
UUID uuid = UUID.randomUUID();
String json = defaultJsonb.toJson(uuid);
UUID result = defaultJsonb.fromJson(json, UUID.class);
assertEquals(uuid, result);
}

@Test
public void serializeObjectWithPth() {

Path expectedPath = Paths.get("/tmp/hello/me.txt");
String expectedPathString = expectedPath.toString().replace("\\", "\\\\");
String expectedJson = "{\"path\":\"" + expectedPathString + "\"}";
final ObjectWithPath objectWithPath = new ObjectWithPath();
objectWithPath.path = expectedPath;
final String s = defaultJsonb.toJson(objectWithPath);
assertEquals(expectedJson, s);

ObjectWithPath actualObject = defaultJsonb.fromJson(expectedJson, ObjectWithPath.class);
assertEquals(expectedPath, actualObject.path);
}

public static class ObjectWithPath {
public Path path;
}
}

0 comments on commit 7980412

Please sign in to comment.