Java Code Examples for org.nem.core.utils.StringEncoder

The following examples show how to use org.nem.core.utils.StringEncoder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: nem.core   Source File: BinaryDeserializer.java    License: MIT License 5 votes vote down vote up
@Override
protected String readOptionalStringImpl(final String label) {
	return this.readIfNotEmpty(() -> {
		final byte[] bytes = this.readOptionalBytes(label);
		return null == bytes ? null : StringEncoder.getString(bytes);
	});
}
 
Example 2
Source Project: nem.core   Source File: JsonSerializerTest.java    License: MIT License 5 votes vote down vote up
@Test
public void serializeToBytesProducesSameBytesAsEntitySerialize() throws Exception {
	// Arrange:
	final JsonSerializer serializer = new JsonSerializer();
	final SerializableEntity entity = new MockSerializableEntity(17, "foo", 42000000000L);
	entity.serialize(serializer);
	final JSONObject expectedJsonObject = serializer.getObject();

	// Act:
	final byte[] resultingBytes = JsonSerializer.serializeToBytes(entity);
	final JSONObject resultingJsonObject = (JSONObject)JSONValue.parse(StringEncoder.getString(resultingBytes));

	// Act / Assert:
	Assert.assertThat(resultingJsonObject, IsEqual.equalTo(expectedJsonObject));
}
 
Example 3
Source Project: nem.deploy   Source File: JsonSerializationPolicyTest.java    License: MIT License 5 votes vote down vote up
@Test
public void toBytesCreatesJsonStringWithTerminatingNewline() {
	// Arrange:
	final JsonSerializationPolicy policy = new JsonSerializationPolicy(null);
	final MockSerializableEntity originalEntity = new MockSerializableEntity(7, "foo", 3);

	// Act:
	final String jsonString = StringEncoder.getString(policy.toBytes(originalEntity));

	// Assert:
	Assert.assertThat(jsonString.endsWith("\r\n"), IsEqual.equalTo(true));
}
 
Example 4
Source Project: nem.deploy   Source File: JsonSerializationPolicyTest.java    License: MIT License 5 votes vote down vote up
private static void assertWriteCanRoundTripEntityWithString(final String str) {
	// Arrange:
	final JsonSerializationPolicy policy = new JsonSerializationPolicy(null);
	final MockSerializableEntity originalEntity = new MockSerializableEntity(7, str, 3);

	// Act:
	final String jsonString = StringEncoder.getString(policy.toBytes(originalEntity));
	final JsonDeserializer deserializer = new JsonDeserializer((JSONObject)JSONValue.parse(jsonString), null);
	final MockSerializableEntity entity = new MockSerializableEntity(deserializer);

	// Assert:
	Assert.assertThat(entity, IsEqual.equalTo(originalEntity));
}
 
Example 5
Source Project: nem.core   Source File: BinarySerializer.java    License: MIT License 4 votes vote down vote up
@Override
protected void writeStringImpl(final String label, final String s) {
	this.writeBytes(label, null == s ? null : StringEncoder.getBytes(s));
}