[scala] YAML parser in Scala

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[scala] YAML parser in Scala

Andrey
Hi,
I am the creator and maintainer of a YAML parser for Java
(http://code.google.com/p/snakeyaml/)
I am impressed by the possibilities available in Scala and I plan to
port the library to Scala
to have a better interface for those who may need it.

Is anybody using YAML in Scala ? What are the problems ? What can be improved ?
Can somebody propose the general interface for the Scala version ?

Since it will be a new application I plan to support Scala 2.8.0 onwards.

You can also create an enhancements request
(http://code.google.com/p/snakeyaml/issues/list)

Regards,
Andrey
Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

Doug Tangren
haven't tried it out yet but this guy's got one http://github.com/daltontf/scala-yaml

-Doug Tangren
http://lessis.me


On Fri, Mar 5, 2010 at 11:24 AM, Andrey Somov <[hidden email]> wrote:
Hi,
I am the creator and maintainer of a YAML parser for Java
(http://code.google.com/p/snakeyaml/)
I am impressed by the possibilities available in Scala and I plan to
port the library to Scala
to have a better interface for those who may need it.

Is anybody using YAML in Scala ? What are the problems ? What can be improved ?
Can somebody propose the general interface for the Scala version ?

Since it will be a new application I plan to support Scala 2.8.0 onwards.

You can also create an enhancements request
(http://code.google.com/p/snakeyaml/issues/list)

Regards,
Andrey

Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

Doug Tangren
also, a common lib for handling scala configs is configgy - http://www.lag.net/configgy/

-Doug Tangren
http://lessis.me


On Fri, Mar 5, 2010 at 11:34 AM, Doug Tangren <[hidden email]> wrote:
haven't tried it out yet but this guy's got one http://github.com/daltontf/scala-yaml

-Doug Tangren
http://lessis.me



On Fri, Mar 5, 2010 at 11:24 AM, Andrey Somov <[hidden email]> wrote:
Hi,
I am the creator and maintainer of a YAML parser for Java
(http://code.google.com/p/snakeyaml/)
I am impressed by the possibilities available in Scala and I plan to
port the library to Scala
to have a better interface for those who may need it.

Is anybody using YAML in Scala ? What are the problems ? What can be improved ?
Can somebody propose the general interface for the Scala version ?

Since it will be a new application I plan to support Scala 2.8.0 onwards.

You can also create an enhancements request
(http://code.google.com/p/snakeyaml/issues/list)

Regards,
Andrey


Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

Tim Dalton-3
In reply to this post by Doug Tangren
Andrey,

I'm the guy with the github project. You are more welcome to use my work as starting point. It is far from a complete implementation, just an exercise in writing parsers that I put out on github to see if it would generate any interest.

It seems to me that Scala's built-in support for XML means there is less demand for YAML support in Scala.

Tim

On 3/5/2010 10:34 AM, Doug Tangren wrote:
haven't tried it out yet but this guy's got one http://github.com/daltontf/scala-yaml

-Doug Tangren
http://lessis.me


On Fri, Mar 5, 2010 at 11:24 AM, Andrey Somov <[hidden email]> wrote:
Hi,
I am the creator and maintainer of a YAML parser for Java
(http://code.google.com/p/snakeyaml/)
I am impressed by the possibilities available in Scala and I plan to
port the library to Scala
to have a better interface for those who may need it.

Is anybody using YAML in Scala ? What are the problems ? What can be improved ?
Can somebody propose the general interface for the Scala version ?

Since it will be a new application I plan to support Scala 2.8.0 onwards.

You can also create an enhancements request
(http://code.google.com/p/snakeyaml/issues/list)

Regards,
Andrey


Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

Andrey
Thank you Tim.
But I have already the complete YAML 1.1 parser.
I just wanted to introduce Scala  interface.
Apparently there is no need. Is it because perfect Java-Scala
interoperability or just no one is using YAML in Scala ?


On Sat, Mar 6, 2010 at 4:32 AM, Tim Dalton <[hidden email]> wrote:

> Andrey,
>
> I'm the guy with the github project. You are more welcome to use my work as
> starting point. It is far from a complete implementation, just an exercise
> in writing parsers that I put out on github to see if it would generate any
> interest.
>
> It seems to me that Scala's built-in support for XML means there is less
> demand for YAML support in Scala.
>
> Tim
>
Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

David Copeland
I would not say there is no need for YAML; it is a useful tool to have
in the toolbox, and provides a needed and useful alternative to XML
and JSON for serialization; that type info can be encoded in YAML
makes it a good fit for this purpose in Scala, given Scala's focus on
typing.  Further, it is a much more widely-used format than
Configgy's, so it seems like a benefit to have a scala-ized interface
to your parser.

To answer your question on interface design, I have only used your
library very briefly in a side project, but I would think that the
manifests feature of 2.8 (and implicit objects/params) might allow
your Scala API to require less type information being passed-in than
the Java version.

Dave

---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com




On Sat, Mar 6, 2010 at 1:23 PM, Andrey Somov <[hidden email]> wrote:

> Thank you Tim.
> But I have already the complete YAML 1.1 parser.
> I just wanted to introduce Scala  interface.
> Apparently there is no need. Is it because perfect Java-Scala
> interoperability or just no one is using YAML in Scala ?
>
>
> On Sat, Mar 6, 2010 at 4:32 AM, Tim Dalton <[hidden email]> wrote:
>> Andrey,
>>
>> I'm the guy with the github project. You are more welcome to use my work as
>> starting point. It is far from a complete implementation, just an exercise
>> in writing parsers that I put out on github to see if it would generate any
>> interest.
>>
>> It seems to me that Scala's built-in support for XML means there is less
>> demand for YAML support in Scala.
>>
>> Tim
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

Andrey
> I would not say there is no need for YAML;
I do not say there is no need for YAML. I say there is no need to
spend time to port a Java library to Scala.

At the moment I do not posses enough knowledge in Scala to design a
flexible and powerful API.
I know SnakeYAML's limitations comparing to PyYAML. In Python they use
mixins (via multiple inheritance), duck typing, metaclasses, functions
as first-class objects, generators (continuations ?). Since nothing is
available in Java I had to find a compromise. The solution is not
always effective. Scala's powerful type system may help but it would
require to re-design the code.
Another approach can be to provide just another facade for the
existing core. But it may limit the possibilities.
I think I will better wait. (contributions/suggestions are always welcome !)

-
Andrey



On Sat, Mar 6, 2010 at 9:59 PM, David Copeland <[hidden email]> wrote:

> I would not say there is no need for YAML; it is a useful tool to have
> in the toolbox, and provides a needed and useful alternative to XML
> and JSON for serialization; that type info can be encoded in YAML
> makes it a good fit for this purpose in Scala, given Scala's focus on
> typing.  Further, it is a much more widely-used format than
> Configgy's, so it seems like a benefit to have a scala-ized interface
> to your parser.
>
> To answer your question on interface design, I have only used your
> library very briefly in a side project, but I would think that the
> manifests feature of 2.8 (and implicit objects/params) might allow
> your Scala API to require less type information being passed-in than
> the Java version.
>
> Dave
>
Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

gjain
This post was updated on .
In reply to this post by Andrey
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

astubbs
gjain wrote
we are trying to use SnakeYaml in scala but getting following problems:


When we are dumping any objects in yaml file, it throws exceptions in scala List class (cannot found Bean properties - No JavaBean properties found in "scala.$colon$colon") and same for org.joda.time.DateTime  class.
Hi, I am from this team.

I have fixed the problem using two custom Representer(s), one for the Scala List and one for the Joda DateTime object. I would like to contribute them back.

Note that I "fixed" my username in the patch, so the NodeID may not be valid (I'm a gitter, not a hg'user, so I'm not sure how to "fix" the author of a commit already existing).

Note, that the Scala code also requires the Scala-JavaTools library, which I haven't patched into the POM. The Scala Representer should probably be in a sub-module, unless you don't mind raping your build, or trying to convert it to Java code :)

These should also be split in two - sorry :/

# HG changeset patch
# User antony.stubbs@gmail.com
# Date 1275116346 -43200
# Node ID c361e5675ef1146c0df3f6590b75f1ce06facf53
# Parent  134ff5bf4e63f0ba553aa60fe35ce63b43501983
Custom Representers for Scala List and JodaTime DateTime

Both written in Scala. Probably need to go into a sub-module of some kind.

Also had to copy the implementation of addTag as it was private. Also needed to use the same package name because the Representer class is package private.

Not entirely sure this was the right way to do it, but it works great for us.

diff -r 134ff5bf4e63 -r c361e5675ef1 AUTHORS
--- a/AUTHORS Fri May 28 02:17:23 2010 +0200
+++ b/AUTHORS Sat May 29 18:59:06 2010 +1200
@@ -23,3 +23,4 @@
 Magne at edb
 infinity0x
 obastard
+Antony Stubbs <antony.stubbs@gmail.com> - Scala List and JodaTime DateTime Representer
\ No newline at end of file
diff -r 134ff5bf4e63 -r c361e5675ef1 src/main/java/org/yaml/snakeyaml/representer/CustomRepresenter.scala
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/yaml/snakeyaml/representer/CustomRepresenter.scala Sat May 29 18:59:06 2010 +1200
@@ -0,0 +1,21 @@
+package org.yaml.snakeyaml.representer
+
+import org.scala_tools.javautils.Implicits._
+
+import org.yaml.snakeyaml.nodes.{Node, Tag}
+import org.joda.time.{DateTimeZone, DateTime}
+
+/**
+ * @author Antony Stubbs
+ */
+class CustomRepresenter extends Representer {
+
+  def getTag(clazz:Class[_], defaultTag:Tag):Tag = {
+    if (classTags.containsKey(clazz)) {
+      return classTags.get(clazz)
+    } else {
+      return defaultTag
+    }
+  }
+
+}
\ No newline at end of file
diff -r 134ff5bf4e63 -r c361e5675ef1 src/main/java/org/yaml/snakeyaml/representer/JodaTimeRepresenter.scala
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/yaml/snakeyaml/representer/JodaTimeRepresenter.scala Sat May 29 18:59:06 2010 +1200
@@ -0,0 +1,84 @@
+package org.yaml.snakeyaml.representer
+
+import org.scala_tools.javautils.Implicits._
+
+import org.yaml.snakeyaml.nodes.{Node, Tag}
+import org.joda.time.{DateTimeZone, DateTime}
+
+/**
+ * @author Antony Stubbs
+ */
+trait JodaTimeRepresenter extends CustomRepresenter {
+
+  multiRepresenters.put(classOf[DateTime], new RepresentJodaDateTime())
+
+  private class RepresentJodaDateTime extends Represent {
+
+    def representData(data:Object):Node = {
+      val dt:DateTime = data.asInstanceOf[DateTime]
+      val years = dt.getYear
+      val months = dt.getMonthOfYear
+      val days = dt.getDayOfMonth
+      val hour24 = dt.getHourOfDay
+      val minutes = dt.getMinuteOfHour
+      val seconds = dt.getSecondOfMinute
+      val millis = dt.getMillisOfSecond
+      var buffer = new StringBuilder(String.valueOf(years))
+      buffer.append("-")
+      if (months < 10) {
+        buffer.append("0")
+      }
+      buffer.append(String.valueOf(months))
+      buffer.append("-")
+      if (days < 10) {
+        buffer.append("0")
+      }
+      buffer.append(String.valueOf(days))
+      buffer.append("T")
+      if (hour24 < 10) {
+        buffer.append("0")
+      }
+      buffer.append(String.valueOf(hour24))
+      buffer.append(":")
+      if (minutes < 10) {
+        buffer.append("0")
+      }
+      buffer.append(String.valueOf(minutes))
+      buffer.append(":")
+      if (seconds < 10) {
+        buffer.append("0")
+      }
+      buffer.append(String.valueOf(seconds))
+      if (millis > 0) {
+        if (millis < 10) {
+            buffer.append(".00")
+        } else if (millis < 100) {
+            buffer.append(".0")
+        } else {
+            buffer.append(".")
+        }
+        buffer.append(String.valueOf(millis))
+      }
+      if (DateTimeZone.UTC.equals(dt.getZone)) {
+        buffer.append("Z");
+      } else {
+        // Get the Offset from GMT taking DST into account
+        var gmtOffset = dt.getZone().getOffset(dt)
+        var minutesOffset = gmtOffset / (60 * 1000);
+        var hoursOffset = minutesOffset / 60;
+        var partOfHour = minutesOffset % 60;
+        if(hoursOffset > 0){
+          buffer.append("+")
+        }
+        buffer.append(hoursOffset + ":")
+        if(partOfHour < 10 ){
+          buffer.append("0" + partOfHour)
+        } else {
+          buffer.append(partOfHour)
+        }
+      }
+      return representScalar(getTag(data.getClass(), Tag.TIMESTAMP), buffer.toString(), null)
+    }
+  }
+
+}
\ No newline at end of file
diff -r 134ff5bf4e63 -r c361e5675ef1 src/main/java/org/yaml/snakeyaml/representer/ScalaRepresenter.scala
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/yaml/snakeyaml/representer/ScalaRepresenter.scala Sat May 29 18:59:06 2010 +1200
@@ -0,0 +1,25 @@
+package org.yaml.snakeyaml.representer
+
+import org.scala_tools.javautils.Implicits._
+
+import org.yaml.snakeyaml.nodes.{Node, Tag}
+import org.joda.time.{DateTimeZone, DateTime}
+
+/**
+ * @author Antony Stubbs
+ */
+trait ScalaRepresenter extends CustomRepresenter {
+
+  multiRepresenters.put(classOf[::[Object]], new RepresentList())
+
+  private class RepresentList extends Represent {
+
+    def representData(data:Object):Node = {
+      val scalaList = data.asInstanceOf[List[_ <: Object]]
+      val javaList = scalaList.asJava
+      return representSequence(getTag(javaList.getClass(), Tag.SEQ), javaList, null)
+    }
+
+  }
+
+}
\ No newline at end of file
Reply | Threaded
Open this post in threaded view
|

Re: [scala] YAML parser in Scala

Andrey
Hi,
thank you for your contribution.

I have added JodaTime example to SnakeYAML (http://code.google.com/p/snakeyaml/wiki/readme#History)

I have started http://code.google.com/p/helicalyaml/ to simplify YAML usage in Scala.
Your feedback is appreciated!

N.B. Please do not use this thread as your topic deviates from the original topic. Either start another thread or use SnakeYAML or HelicalYAML projects.