From a2d48a5896335a567ddb21ed87fe38cd65d4764d Mon Sep 17 00:00:00 2001 From: Fabian Schuiki Date: Fri, 25 Mar 2022 19:53:43 +0100 Subject: Fix anno deserialization when class field is not first (#2501) Update `findTypeHints` to allow for the "class" field in JSON objects to appear anywhere in the object. This used to rely on the field being the very first in the object, which is easily violated when reading JSON data generated externally, since an object's order of fields is unspecified and can be arbitrarily scrambled. Fixes #2497.--- src/test/scala/firrtl/JsonProtocolSpec.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/test') diff --git a/src/test/scala/firrtl/JsonProtocolSpec.scala b/src/test/scala/firrtl/JsonProtocolSpec.scala index 0da43204..3e07542b 100644 --- a/src/test/scala/firrtl/JsonProtocolSpec.scala +++ b/src/test/scala/firrtl/JsonProtocolSpec.scala @@ -31,6 +31,8 @@ object JsonProtocolTestClasses { with HasSerializationHints { def typeHints = Seq(param.getClass) } + + case class SimpleAnnotation(alpha: String) extends NoTargetAnnotation } import JsonProtocolTestClasses._ @@ -68,4 +70,14 @@ class JsonProtocolSpec extends AnyFlatSpec { val deserAnno = serializeAndDeserialize(anno) assert(anno == deserAnno) } + + "JSON object order" should "not affect deserialization" in { + val anno = SimpleAnnotation("hello") + val serializedAnno = """[{ + "alpha": "hello", + "class": "firrtlTests.JsonProtocolTestClasses$SimpleAnnotation" + }]""" + val deserAnno = JsonProtocol.deserialize(serializedAnno).head + assert(anno == deserAnno) + } } -- cgit v1.2.3