Issue sending mails to the group/JSON library prototype

11 views
Skip to first unread message

Marc Esquerrà

unread,
Sep 25, 2013, 10:50:22 AM9/25/13
to scala-...@googlegroups.com
Hi all,

I've been working on an open source project to create a library to work with JSON documents with Scala. Now that I have a working prototype I wanted to share it with you, so I thought I could send an email to the group. But somethings seams to have gone wrong.

Yesterday, I sent this presentation email to lsug...@meetup.com but it did not make to the mailing list (http://www.meetup.com/london-scala/messages/archive/). Today, I've tried to send it to the google group (scala-...@googlegroups.com), but it has not made to the mailing list. Again.

Now, I'm trying to send the message through the google group it self. Do you know if there is something that may prevent me to send emails to either of the groups?

In any case, I'll paste my email here for your consideration :)

Cheers,
Marc

----

Hi all,

I would like to share with you a project I've been working on: a Scala library to work with JSON documents. I know, the idea may be not the most original but I'm sure that you would find some innovative ideas on how it's implemented and on how to use it. I call it KissJson.

Feel free to download it and try it. And please... any comment or collaboration would be really, really appreciated :)


Since you are almost the first people I share this project with and since I'm writing this email from what I get fresh out of my mind, please excuse me if It's not as clear as It should be... or if I write too much.


Well, let's go.


First of all, if you take a look at the name I've given the library you can see that I'm huge fan of the Kiss principle (Keep it Simple) and that is exactly what I've tried to achieve implementing this.

Also, the full concept and the main design principles of the library revolve around the following four concepts:
  • A Scala representation of a JSON document (JsonValue, JsonString, ...)
  • A DSL to write JSON "literals" directly in the scala source code
  • A set of bidirectional JsonValue convertors
    • from string representation
    • from scala types (base types, case classes, arrays, collections, options, ...)
  • Tools to work directly with the JsonValues




JsonValue

The following types conform the core of the library:

JsonValue:  The base type of all JSON values
JsonBoolean, JsonString, JsonNumber, JsonInteger, JsonReal
JsonArray
JsonObject
JsonNull


This collection of types have been designed to replicate what Option, Some and None do in standard Scala.

Option  →  JsonValue
None  →  JsonNull
Some  →  All the others

But, with a really important difference, contrarily to what happens with Option this is perfectly valid in KissJson:

val str: JsonString = JsonNull

This allows to keep the concept of "null" in the JSON sense of it, combined with the clean versatility and safety of working with an "Option" like pattern:

val result = str.getOrElse("defaultString")

This way, JsonArray wraps a Vector of JsonValues (if asked for an invalid index it returns JsonNull) and JsonObject wraps a Map of String → JsonValue (which also, if asked for an invalid key will return JsonNull).



Another cool feature of KissJson is the way you can ask for an specific key of a JsonObject. I wanted it to be idiomatic, like with the usage of dynamic types (being able to make queries of the like of obj.company.name) and also to keep the safety provided by the compiler (detecting the error if I write "obj.tostring" instead of "obj.toString"). To achieve that, I've created the ".?." accessor so you can do this:

val name: JsonValue = obj.?.company.?.name





Literals

This is the bit I'm probably most proud of. An example is the best explanation in this case. And that the example explains by itself probably 's why I'm so proud ;)

val json = J(

    `type`     = "Sample Json Object",

     id        = 12,

     rating    = 3.7,

     content   = "characters",

     enabled   = true,

     items     = J("Spock", "Luck")

)



Conversors

A JSON library wouldn't make much sense if you can't get data from a String representation or render your data into a String, ready for sending. With KissJson the parsing works like this:

val jsonObjectString =

"""

{

"name": "John",

"surname": "Whatson"

}

"""


val jsonObject:Try[JsonValue] = jsonObjectString.asJson

And rendering back to string is done with the render method:

val msg: String = jsonValue.render


Similarly, it's really handy to be able to convert jsonValues to standar scala clases:

case class Person(name: String, age: Int)
val p = Person("John", 32)
val r: Try[JsonValue] = p.asJson

and vice-versa:

val p2 = J(name = "Jhon", age = 32)
val r2: Try[Person] = p2.as[Person]




Ok, I think this is enough for a presentation. As you can see the library is really easy to use and I think it's also quite powerful. There are several things to polish, but It has a lot of potential. I wish you find both this email and KissJson useful :)


Kind regards,

Marc Esquerrà i Bayo
@marcesquerra
Reply all
Reply to author
Forward
0 new messages