diff options
| author | David Biancolin | 2019-10-29 20:56:01 -0700 |
|---|---|---|
| committer | David Biancolin | 2019-10-29 20:56:01 -0700 |
| commit | 66464f5e4c37bbfa3fc90da3dde964dd0410cfd1 (patch) | |
| tree | 19f22437176b6e2d7359dd1447295a3a21dd84bf /src | |
| parent | 7d16fb5f03812b56addfc7450f0808ccc54530c0 (diff) | |
Check that all annotations provide the typeHint
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/firrtl/annotations/JsonProtocol.scala | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/main/scala/firrtl/annotations/JsonProtocol.scala b/src/main/scala/firrtl/annotations/JsonProtocol.scala index 02ec041a..1c84152e 100644 --- a/src/main/scala/firrtl/annotations/JsonProtocol.scala +++ b/src/main/scala/firrtl/annotations/JsonProtocol.scala @@ -113,14 +113,17 @@ object JsonProtocol { } // Recursively gather typeHints by pulling the "class" field from JObjects // Json4s should emit this as the first field in all serialized classes - def findTypeHints(classInst: Seq[JValue]): Seq[String] = classInst.flatMap({ + // Setting requireClassField mandates that all JObjects must provide a typeHint, + // this used on the first invocation to check all annotations do so + def findTypeHints(classInst: Seq[JValue], requireClassField: Boolean = false): Seq[String] = classInst.flatMap({ case JObject(("class", JString(name)) :: fields) => name +: findTypeHints(fields.map(_._2)) - case obj: JObject => throw new InvalidAnnotationJSONException(s"Expected field 'class' not found! $obj") + case obj: JObject if requireClassField => throw new InvalidAnnotationJSONException(s"Expected field 'class' not found! $obj") + case JObject(fields) => findTypeHints(fields.map(_._2)) case JArray(arr) => findTypeHints(arr) case oJValue => Seq() }).distinct - val classes = findTypeHints(annos) + val classes = findTypeHints(annos, true) val loaded = classes.map(Class.forName(_).asInstanceOf[Class[_]]) implicit val formats = jsonFormat(loaded) read[List[Annotation]](in) |
