Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for unicode toString in JSONWire fixes #934 #981

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/JSONWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -1291,4 +1291,14 @@ public boolean useTypes() {
return useTypes;
}
}

/**
* Render as a UTF-8 string.
*
* @return a UTF-8 string representation of the wire data.
*/
@Override
public String toString() {
return toUtf8String();
}
}
18 changes: 18 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/TextWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,24 @@ public String toString() {
return bytes.toString();
}

/**
* Converts the underlying bytes of this TextWire to its ISO-8859-1 string representation.
*
* @return A string representation of the TextWire's underlying bytes in ISO-8859-1 encoding.
*/
public String to8bitString() {
return bytes.to8bitString();
}

/**
* Converts the underlying bytes of this TextWire to its UTF-8 string representation.
*
* @return A string representation of the TextWire's underlying bytes in UTF-8 encoding.
*/
public String toUtf8String() {
return bytes.toUtf8String();
}

@Override
public void copyTo(@NotNull WireOut wire) throws InvalidMarshallableException {
if (wire instanceof TextWire || wire instanceof YamlWire) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/net/openhft/chronicle/wire/WireType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.Map;
import java.util.Spliterator;
Expand Down Expand Up @@ -200,6 +199,11 @@ public Wire apply(@NotNull Bytes<?> bytes) {
public boolean isText() {
return true;
}

@Override
public String asString(Object marshallable) {
return asUtf8String(marshallable);
}
},
JSON_ONLY {
@NotNull
Expand All @@ -212,6 +216,11 @@ public Wire apply(@NotNull Bytes<?> bytes) {
public boolean isText() {
return true;
}

@Override
public String asString(Object marshallable) {
return asUtf8String(marshallable);
}
},
YAML {
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -282,6 +291,17 @@ public Wire apply(@NotNull Bytes<?> bytes) {
// Size after which data is compressed.
private static final int COMPRESSED_SIZE = Integer.getInteger("WireType.compressedSize", 128);

protected @NotNull String asUtf8String(Object marshallable) {
ValidatableUtil.startValidateDisabled();
try (ScopedResource<Bytes<Void>> stlBytes = Wires.acquireBytesScoped()) {
final Bytes<?> bytes = stlBytes.get();
asBytes(marshallable, bytes);
return bytes.toUtf8String();
} finally {
ValidatableUtil.endValidateDisabled();
}
}

/**
* Determines the of a given {@link Wire} instance. This method inspects
* the underlying type of the provided wire instance and maps it to its corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2016-2022 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.wire;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
* Verify that unicode characters can be properly represented in JSON output.
*/
public class JsonWireToStringAcceptanceTest {

private static Collection<WireType> WIRE_TYPES = Arrays.asList(WireType.JSON, WireType.JSON_ONLY);

@ParameterizedTest
@ValueSource(strings = {"£", "€", "¥", "₹", "ó", "óaóó", "☞☞☞☞☞", "ÊÆÄ"})
public void json_verifyAsString(String input) {
Map<String, String> map = new HashMap<>();
map.put("x", input);
for (WireType wireType : WIRE_TYPES) {
assertEquals("{\"x\":\"" + input + "\"}", wireType.asString(map));
}
}

@ParameterizedTest
@ValueSource(strings = {"£", "€", "¥", "₹", "ó", "óaóó"})
public void json_verifyObjectToString(String input) {
Map<String, String> map = new HashMap<>();
map.put("x", input);
WireOut object = new JSONWire().getValueOut().object(map);
assertEquals("{\"x\":\"" + input + "\"}", object.toString());
}

@ParameterizedTest
@ValueSource(strings = {"£", "€", "¥", "₹", "ó", "óaóó"})
public void json_verifyAsText(String input) {
Map<String, String> map = new HashMap<>();
map.put("x", input);
JSONWire jsonWire = new JSONWire();
jsonWire.getValueOut().object(map);
assertEquals("{\"x\":\"" + input + "\"}", JSONWire.asText(jsonWire));
}

}