-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathHammerObject.scala
62 lines (52 loc) · 1.43 KB
/
HammerObject.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// See LICENSE for licence details.
package hammer_ir
import scala.reflect.ClassTag
import play.api.libs.json.{Json, JsObject, JsValue}
/**
* All Hammer IR types that can be converted to a JsValue.
*/
trait JSONConvertible {
/**
* Turn this object into a Play JSON object.
*/
private[hammer_ir] def toJSON: JsValue
}
/**
* All Hammer IR objects implement this trait which allows them to be
* serialized to/deserialized from JSON.
*/
trait HammerObject {
/**
* Turn this object into a Play JSON object.
*/
private[hammer_ir] def toJSON: JsObject
/**
* Turn this object into a JSON string.
*/
override def toString = Json.prettyPrint(toJSON)
/**
* Write this object as a JSON string into the given file.
*/
def toFile(filename: String) = reflect.io.File(filename).writeAll(toString)
}
/**
* Abstract class mixed into companion classes of Hammer IR objects.
*/
abstract class HammerObjectCompanion[T <: HammerObject : ClassTag] {
/**
* Create this object from a Play JSON object.
*/
private[hammer_ir] def fromJSON(json: JsObject): T
/**
* Create this object from a JSON string.
*/
def fromString(json: String): T = fromJSON(Json.parse(json).as[JsObject])
/**
* Create this object from a JSON file.
*/
def fromFile(filename: String): T = {
val source = scala.io.Source.fromFile(filename)
val lines = try source.mkString finally source.close()
fromString(lines)
}
}