WIP Resource event handling
[aquarium] / src / main / scala / gr / grnet / aquarium / converter / JsonConversions.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.converter
37
38 import net.liftweb.json.JsonAST.JValue
39 import net.liftweb.json._
40 import ext.JodaTimeSerializers
41
42 import gr.grnet.aquarium.util.{makeString, UTF_8_Charset}
43 import java.nio.charset.Charset
44
45 /**
46  * Provides conversion methods from and to JSON.
47  *
48  * The underlying library used is lift-json.
49  *
50  * @author Christos KK Loverdos <loverdos@gmail.com>
51  */
52
53 object JsonConversions {
54   /**
55    * The application-wide JSON formats used from the underlying lift-json library.
56    */
57   implicit final val Formats = (DefaultFormats ++ JodaTimeSerializers.all)
58 //  implicit final val Formats = (DefaultFormats.withHints(FullTypeHints(List(classOf[AnyRef]))) ++ JodaTimeSerializers.all)
59 //  Serialization.formats(FullTypeHints(List(classOf[AnyRef])))
60
61   /**
62    * Converts a value to JSON AST (Abstract Syntax Tree) by acting a bit intelligently, depending on the actual type
63    * of the given value. In particular, if the given value `any` is already a JSON AST, it is returned
64    * as is. Also, if the given value `any` is a String, it is assumed that the string is already a JSON
65    * representation and then it is parsed to a JSON AST.
66    */
67   final def anyToJValue(any: Any)(implicit formats: Formats = Formats): JValue = {
68     any match {
69       case jValue: JValue ⇒
70         jValue
71       case json: String ⇒
72         jsonToJValue(json)
73       case jsonFormat: JsonTextFormat ⇒
74         jsonToJValue(jsonFormat.value)
75       case _ ⇒
76         Extraction.decompose(any)
77     }
78   }
79
80   final def jsonToJValue(json: String): JValue = {
81     parse(json)
82   }
83
84   final def jValueToCompactString(jValue: JValue): String = {
85     Printer.compact(JsonAST.render(jValue))
86   }
87
88   final def jValueToPrettyString(jValue: JValue): String = {
89     Printer.pretty(JsonAST.render(jValue))
90   }
91
92   final def anyToJson(any: Any, pretty: Boolean = true)(implicit formats: Formats = Formats): String = {
93     val jValue = anyToJValue(any)
94     val jDoc = JsonAST.render(jValue)
95     if(pretty) {
96       Printer.pretty(jDoc)
97     } else {
98       Printer.compact(jDoc)
99     }
100   }
101
102   final def jsonBytesToJson(bytes: Array[Byte], charset: Charset = UTF_8_Charset): String = {
103     makeString(bytes, charset)
104   }
105
106   final def jsonBytesToObject[A: Manifest](bytes: Array[Byte], charset: Charset = UTF_8_Charset): A = {
107     val json = jsonBytesToJson(bytes, charset)
108     jsonToObject[A](json)
109   }
110
111   final def jsonToObject[A](json: String)(implicit ma: Manifest[A], formats: Formats = Formats): A = {
112     val jValue = parse(json)
113     jValueToObject[A](jValue)
114   }
115
116   final def jValueToObject[A](jValue: JValue)(implicit ma: Manifest[A], formats: Formats = Formats): A = {
117     Extraction.extract[A](jValue)
118   }
119 }