src/Pure/General/json.scala
changeset 73340 0ffcad1f6130
parent 71776 5ef7f374e0f8
child 75252 41dfe941c3da
equal deleted inserted replaced
73339:9efdebe24c65 73340:0ffcad1f6130
   174 
   174 
   175     def apply(json: T): S =
   175     def apply(json: T): S =
   176     {
   176     {
   177       val result = new StringBuilder
   177       val result = new StringBuilder
   178 
   178 
   179       def string(s: String)
   179       def string(s: String): Unit =
   180       {
   180       {
   181         result += '"'
   181         result += '"'
   182         result ++=
   182         result ++=
   183           s.iterator.map {
   183           s.iterator.map {
   184             case '"'  => "\\\""
   184             case '"'  => "\\\""
   193               else c
   193               else c
   194           }.mkString
   194           }.mkString
   195         result += '"'
   195         result += '"'
   196       }
   196       }
   197 
   197 
   198       def array(list: List[T])
   198       def array(list: List[T]): Unit =
   199       {
   199       {
   200         result += '['
   200         result += '['
   201         Library.separate(None, list.map(Some(_))).foreach({
   201         Library.separate(None, list.map(Some(_))).foreach({
   202           case None => result += ','
   202           case None => result += ','
   203           case Some(x) => json_format(x)
   203           case Some(x) => json_format(x)
   204         })
   204         })
   205         result += ']'
   205         result += ']'
   206       }
   206       }
   207 
   207 
   208       def object_(obj: Object.T)
   208       def object_(obj: Object.T): Unit =
   209       {
   209       {
   210         result += '{'
   210         result += '{'
   211         Library.separate(None, obj.toList.map(Some(_))).foreach({
   211         Library.separate(None, obj.toList.map(Some(_))).foreach({
   212           case None => result += ','
   212           case None => result += ','
   213           case Some((x, y)) =>
   213           case Some((x, y)) =>
   216             json_format(y)
   216             json_format(y)
   217         })
   217         })
   218         result += '}'
   218         result += '}'
   219       }
   219       }
   220 
   220 
   221       def json_format(x: T)
   221       def json_format(x: T): Unit =
   222       {
   222       {
   223         x match {
   223         x match {
   224           case null => result ++= "null"
   224           case null => result ++= "null"
   225           case _: Int | _: Long | _: Boolean => result ++= x.toString
   225           case _: Int | _: Long | _: Boolean => result ++= x.toString
   226           case n: Double =>
   226           case n: Double =>