package com.dslplatform.json;

import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

public class PrimitivesTest {

	@CompiledJson
	public static class IntPrimitive {
		public int x;
	}

	@CompiledJson
	public static class ImmutableIntPrimitive {
		public final int x;

		public ImmutableIntPrimitive(int x) {
			this.x = x;
		}
	}

	private final DslJson<Object> dslJson = new DslJson<>();

	@Test
	public void cantHaveNullOnPrimitive() throws IOException {
		byte[] input = "{\"x\":null}".getBytes("UTF-8");
		try {
			dslJson.deserialize(IntPrimitive.class, input, input.length);
			Assert.fail("Expecting exception");
		} catch (IOException ex) {
			Assert.assertTrue(ex.getMessage().contains("Error parsing number at position: 5"));
		}
	}

	@Test
	public void cantHaveNullOnImmutablePrimitive() throws IOException {
		byte[] input = "{\"x\":null}".getBytes("UTF-8");
		try {
			dslJson.deserialize(ImmutableIntPrimitive.class, input, input.length);
			Assert.fail("Expecting exception");
		} catch (IOException ex) {
			Assert.assertTrue(ex.getMessage().contains("Error parsing number at position: 5"));
		}
	}
}

@@ -74,24 +74,63 @@
 it.Test;%0D%0A%0D%0A
+import java.io.ByteArrayOutputStream;%0D%0A
 import java.
@@ -1218,12 +1218,859 @@
 %09%09%7D%0D%0A%09%7D%0D
+%0A%0D%0A%09@CompiledJson%0D%0A%09public static class FloatPrimitive %7B%0D%0A%09%09@JsonAttribute(includeToMinimal = JsonAttribute.IncludePolicy.NON_DEFAULT)%0D%0A%09%09public float f1;%0D%0A%09%09@JsonAttribute(includeToMinimal = JsonAttribute.IncludePolicy.ALWAYS)%0D%0A%09%09public float f2;%0D%0A%09%7D%0D%0A%0D%0A%09@Test%0D%0A%09public void noDefaultValueForFloat() throws IOException %7B%0D%0A%09%09FloatPrimitive fp = new FloatPrimitive();%0D%0A%09%09fp.f1 = 0;%0D%0A%09%09fp.f2 = 0;%0D%0A%09%09DslJson%3CObject%3E minimal = new DslJson%3C%3E(new DslJson.Settings%3C%3E().skipDefaultValues(true).includeServiceLoader());%0D%0A%09%09ByteArrayOutputStream os = new ByteArrayOutputStream();%0D%0A%09%09minimal.serialize(fp, os);%0D%0A%09%09Assert.assertEquals(%22%7B%5C%22f2%5C%22:0.0%7D%22, os.toString(%22UTF-8%22));%0D%0A%09%09FloatPrimitive res = minimal.deserialize(FloatPrimitive.class, os.toByteArray(), os.size());%0D%0A%09%09Assert.assertEquals(fp.f1, res.f1, 0);%0D%0A%09%09Assert.assertEquals(fp.f2, res.f2, 0);%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Test for sanity check for correct float primitives behavior