org.nem.core.utils.StringEncoder Java Examples

The following examples show how to use org.nem.core.utils.StringEncoder. 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 File: BinaryDeserializer.java    From nem.core with 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 File: JsonSerializerTest.java    From nem.core with 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 File: JsonSerializationPolicyTest.java    From nem.deploy with 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 File: JsonSerializationPolicyTest.java    From nem.deploy with 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 File: BinarySerializer.java    From nem.core with 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));
}