diff --git a/modules/core/src/main/scala/tethys/readers/instances/AllJsonReaders.scala b/modules/core/src/main/scala/tethys/readers/instances/AllJsonReaders.scala index 19591382..4b15604d 100644 --- a/modules/core/src/main/scala/tethys/readers/instances/AllJsonReaders.scala +++ b/modules/core/src/main/scala/tethys/readers/instances/AllJsonReaders.scala @@ -29,6 +29,12 @@ trait AllJsonReaders extends OptionReaders { } } + implicit lazy val charReader: JsonReader[Char] = stringReader.mapWithField{ implicit fieldName => { + case s if s.length == 1 => s.head + case s => ReaderError.wrongJson(s"Expected char value but found: $s") + } + } + implicit lazy val numberReader: JsonReader[Number] = new JsonReader[Number] { override def read(it: TokenIterator)(implicit fieldName: FieldName): Number = { if(it.currentToken().isNumberValue) { diff --git a/modules/core/src/main/scala/tethys/writers/instances/AllJsonWriters.scala b/modules/core/src/main/scala/tethys/writers/instances/AllJsonWriters.scala index d7683dd4..5ff10408 100644 --- a/modules/core/src/main/scala/tethys/writers/instances/AllJsonWriters.scala +++ b/modules/core/src/main/scala/tethys/writers/instances/AllJsonWriters.scala @@ -40,6 +40,10 @@ trait AllJsonWriters extends OptionWriters with EitherWriters { override def write(value: String, tokenWriter: TokenWriter): Unit = tokenWriter.writeString(value) } + implicit lazy val charWriter: JsonWriter[Char] = new JsonWriter[Char] { + override def write(value: Char, tokenWriter: TokenWriter): Unit = tokenWriter.writeString(value.toString) + } + implicit lazy val javaIntWriter: JsonWriter[java.lang.Integer] = new JsonWriter[java.lang.Integer] { override def write(value: java.lang.Integer, tokenWriter: TokenWriter): Unit = tokenWriter.writeNumber(value) } diff --git a/modules/core/src/test/scala/tethys/readers/DefaultReadersTest.scala b/modules/core/src/test/scala/tethys/readers/DefaultReadersTest.scala index fe6f66f5..48226e48 100644 --- a/modules/core/src/test/scala/tethys/readers/DefaultReadersTest.scala +++ b/modules/core/src/test/scala/tethys/readers/DefaultReadersTest.scala @@ -24,6 +24,7 @@ class DefaultReadersTest extends AnyFlatSpec { private val cases: List[(TestDefinition[_], List[TokenNode])] = List[(TestDefinition[_], List[TokenNode])]( test("1") -> value("1"), + test('1') -> value("1"), test(1) -> value(1), test(1: Short) -> value(1: Short), test(1L) -> value(1L), diff --git a/modules/core/src/test/scala/tethys/readers/JsonReaderBuilderTest.scala b/modules/core/src/test/scala/tethys/readers/JsonReaderBuilderTest.scala index 9cb96934..4b6e476a 100644 --- a/modules/core/src/test/scala/tethys/readers/JsonReaderBuilderTest.scala +++ b/modules/core/src/test/scala/tethys/readers/JsonReaderBuilderTest.scala @@ -63,6 +63,7 @@ class JsonReaderBuilderTest extends AnyFlatSpec with Matchers { .addField[Boolean]("c") .addField[Seq[String]]("d") .addField[Double]("e") + .addField[Char]("f") .addField[Option[Int]]("opt") .buildReader(FatClass.apply) } @@ -72,13 +73,15 @@ class JsonReaderBuilderTest extends AnyFlatSpec with Matchers { "b" -> "s", "c" -> true, "d" -> arr("a", "b", "c"), - "e" -> 4 + "e" -> 4, + "f" -> "c" )) shouldBe FatClass( a = 1, b = "s", c = true, d = Seq("a", "b", "c"), e = 4.0D, + f = 'c', opt = None ) } @@ -165,5 +168,6 @@ object JsonReaderBuilderTest { c: Boolean, d: Seq[String], e: Double, + f: Char, opt: Option[Int]) } diff --git a/modules/core/src/test/scala/tethys/writers/DefaultWritersTest.scala b/modules/core/src/test/scala/tethys/writers/DefaultWritersTest.scala index ffc26fb0..577f8112 100644 --- a/modules/core/src/test/scala/tethys/writers/DefaultWritersTest.scala +++ b/modules/core/src/test/scala/tethys/writers/DefaultWritersTest.scala @@ -23,6 +23,7 @@ class DefaultWritersTest extends AnyFlatSpec { private val cases: List[(TestDefinition[_], List[TokenNode])] = List[(TestDefinition[_], List[TokenNode])]( test("1") -> value("1"), + test('1') -> value("1"), test(1) -> value(1), test(1: Short) -> value(1: Short), test(1L) -> value(1L), diff --git a/modules/core/src/test/scala/tethys/writers/SimpleJsonObjectWriterTest.scala b/modules/core/src/test/scala/tethys/writers/SimpleJsonObjectWriterTest.scala index ab310da0..59473fc9 100644 --- a/modules/core/src/test/scala/tethys/writers/SimpleJsonObjectWriterTest.scala +++ b/modules/core/src/test/scala/tethys/writers/SimpleJsonObjectWriterTest.scala @@ -4,7 +4,7 @@ import org.scalatest.matchers.should.Matchers.{value => _, _} import org.scalatest.flatspec.AnyFlatSpec import tethys.{JsonObjectWriter, JsonWriter} import tethys.commons.TokenNode._ -import tethys.writers.SimpleJsonObjectWriterTest.TestData +import tethys.writers.SimpleJsonObjectWriterTest.{CharData, TestData} import tethys.writers.instances.SimpleJsonObjectWriter import tethys.writers.tokens.SimpleTokenWriter._ @@ -39,10 +39,22 @@ class SimpleJsonObjectWriterTest extends AnyFlatSpec { "c" -> false ) } + + it should "write correct object with char field" in { + implicit val charWriter: SimpleJsonObjectWriter[CharData] = { + JsonWriter.obj[CharData] + .addField("c")(_.c) + } + + CharData('c').asTokenList shouldBe obj( + "c" -> "c" + ) + } } object SimpleJsonObjectWriterTest { case class TestData(a: Int, b: String) + case class CharData(c: Char) }