scala/fs2json/fs2json.sc
changeset 60 4267602e8494
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scala/fs2json/fs2json.sc	Tue Dec 15 09:22:21 2020 +0100
@@ -0,0 +1,23 @@
+import ammonite.ops._
+import os.FileType.{Dir, File}
+import ujson.{Obj, Str, Value}
+
+import scala.collection.mutable
+
+def fs2json(p: Path): Obj = {
+  val l: Seq[(String, Value)] = ls(p) flatMap { f => f.fileType match {
+    case Dir => Some(f.last -> fs2json(f))
+    case File if f.last endsWith ".json" =>
+      Some(f.last.dropRight(5) -> ujson.read(os.read(f)))
+    case File =>
+      val n = f.last.split("\\.").toList match {
+        case v :: Nil => v
+        case v => v dropRight 1 mkString "."
+      }
+      Some(n -> Str(os.read(f).stripLineEnd))
+    case _ => None
+  }}
+  Obj(mutable.LinkedHashMap.from(l))
+}
+
+// vim: et sw=2 ts=2 syn=scala