-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unicode toString support to JSONWire
- Loading branch information
Showing
4 changed files
with
177 additions
and
2 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
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
87 changes: 87 additions & 0 deletions
87
src/main/java/net/openhft/chronicle/wire/internal/UnicodeToStringHelper.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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.internal; | ||
|
||
import net.openhft.chronicle.bytes.Bytes; | ||
import net.openhft.chronicle.bytes.BytesStore; | ||
import net.openhft.chronicle.bytes.VanillaBytes; | ||
import net.openhft.chronicle.bytes.internal.NativeBytesStore; | ||
import net.openhft.chronicle.core.Jvm; | ||
import net.openhft.chronicle.core.Memory; | ||
import net.openhft.chronicle.core.io.ClosedIllegalStateException; | ||
|
||
import java.nio.BufferUnderflowException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Helper class to convert a stream of bytes to a UTF-8 encoded string. | ||
* Heavily based on the approach used in {@link VanillaBytes#toString()} but with UTF-8 support. | ||
* This is used selectively rather than everywhere because some parts of whire use 8-bit encoding of strings based on | ||
* ISO-8859-1. | ||
*/ | ||
public final class UnicodeToStringHelper { | ||
|
||
private UnicodeToStringHelper() { | ||
} | ||
|
||
/** | ||
* Represent the bytes store as a UTF-8 string. | ||
* | ||
* @return UTF-8 string representation of the bytes store. | ||
*/ | ||
public static String toUnicodeString(BytesStore<?, ?> bytesStore) { | ||
try { | ||
try { | ||
return bytesStore instanceof NativeBytesStore | ||
? toStringNativeBytes((NativeBytesStore<?>) bytesStore) | ||
: toStringBytesStore(bytesStore); | ||
} catch (IllegalStateException e) { | ||
throw Jvm.rethrow(e); | ||
} | ||
} catch (Exception e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
private static String toStringNativeBytes(NativeBytesStore<?> bytesStore) { | ||
final Memory memory = bytesStore.memory; | ||
int length = (int) | ||
Math.min(Bytes.MAX_HEAP_CAPACITY, bytesStore.realReadRemaining()); | ||
byte[] bytes = new byte[length]; | ||
final long address = bytesStore.address + bytesStore.translate(bytesStore.readPosition()); | ||
for (int i = 0; i < length && i < bytesStore.realCapacity(); i++) { | ||
bytes[i] = memory.readByte(address + i); | ||
} | ||
return new String(bytes, StandardCharsets.UTF_8); | ||
} | ||
|
||
private static String toStringBytesStore(BytesStore<?, ?> bytesStore) | ||
throws ClosedIllegalStateException { | ||
int length = (int) Math.min(Bytes.MAX_HEAP_CAPACITY, bytesStore.readRemaining()); | ||
byte[] bytes = new byte[length]; | ||
try { | ||
for (int i = 0; i < length; i++) { | ||
bytes[i] = (bytesStore.readByte(bytesStore.readPosition() + i)); | ||
} | ||
} catch (BufferUnderflowException e) { | ||
// ignored | ||
} | ||
return new String(bytes, StandardCharsets.UTF_8); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/net/openhft/chronicle/wire/JsonWireUnicodeAcceptanceTest.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Verify that unicode characters can be properly represented in JSON output. | ||
*/ | ||
public class JsonWireUnicodeAcceptanceTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"£", "€", "¥", "₹", "ó", "óaóó", "☞☞☞☞☞", "ÊÆÄ"}) | ||
public void json_verifyAsString(String input) { | ||
Map<String, String> map = new HashMap<>(); | ||
map.put("x", input); | ||
assertEquals("{\"x\":\"" + input + "\"}", WireType.JSON.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)); | ||
} | ||
|
||
} |