在Scala.js中解析数据库行的JSON表示形式 [英] Parsing the JSON representation of database rows in Scala.js

查看:76
本文介绍了在Scala.js中解析数据库行的JSON表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scala.js,并且想从我的Postgres数据库中使用json格式的简单提取的行数据.

I am trying out scala.js, and would like to use simple extracted row data in json format from my Postgres database.

这是我想解析为一些强类型的scala集合的json类型的人为示例,要注意的功能是有n行,各种列类型包括一个数组,仅用于覆盖现实生活中的场景,不必担心创建内联表以从中提取JSON的SQL,为了完整性起见,我已将其包括在内,它对scala.js中JSON的解析导致了我的麻烦

Here is an contrived example of the type of json I would like to parse into some strongly typed scala collection, features to note are that there are n rows, various column types including an array just to cover likely real life scenarios, don't worry about the SQL which creates an inline table to extract the JSON from, I've included it for completeness, its the parsing of the JSON in scala.js that is causing me problems

with inline_t as (  
select * from (values('One',1,True,ARRAY[1],1.0),
                     ('Six',6,False,ARRAY[1,2,3,6],2.4494),
                     ('Eight',8,False,ARRAY[1,2,4,8],2.8284)) as v (as_str,as_int,is_odd,factors,sroot))
select json_agg(t) from inline_t t;

 [{"as_str":"One","as_int":1,"is_odd":true,"factors":[1],"sroot":1.0}, 
 {"as_str":"Six","as_int":6,"is_odd":false,"factors":[1,2,3,6],"sroot":2.4494}, 
 {"as_str":"Eight","as_int":8,"is_odd":false,"factors":[1,2,4,8],"sroot":2.8284}]

我认为使用如此处暗示的upickle或prickle之类的东西应该很容易:

I think this should be fairly easy using something like upickle or prickle as hinted at here: How to parse a json string to a case class in scaja.js and vice versa but I haven't been able to find a code example, and I'm not up to speed enough with Scala or Scala.js to work it out myself. I'd be very grateful if someone could post some working code to show how to achieve the above

编辑 这是我尝试过的方法,但是我走得还很远

EDIT This is the sort of thing I've tried, but I'm not getting very far

val jsparsed = scala.scalajs.js.JSON.parse(jsonStr3)

val jsp1 = jsparsed.selectDynamic("1")

val items = jsp1.map{ (item: js.Dynamic) =>
  (item.as_str, item.as_int, item.is_odd, item.factors, item.sroot)
  .asInstanceOf[js.Array[(String, Int, Boolean, Array[Int], Double)]].toSeq
}
println(items._1)

推荐答案

因此,您实际上处于要操作JSON值的境地.由于您没有从头到尾序列化/反序列化Scala值,因此uPickle或Prickle之类的序列化库对您没有太大帮助.

So you are in a situation where you actually want to manipulate JSON values. Since you're not serializing/deserializing Scala values from end-to-end, serialization libraries like uPickle or Prickle will not be very helpful to you.

您可以看一下跨平台的JSON操作库,例如圆圈.这将给您带来的好处是,您根本不需要处理JavaScript数据结构".相反,该库将解析您的JSON并将其公开为Scala数据结构.如果您希望您的代码也可以交叉编译,这可能是最好的选择.

You could have a look at a cross-platform JSON manipulation library, such as circe. That would give you the advantage that you wouldn't have to "deal with JavaScript data structures" at all. Instead, the library would parse your JSON and expose it as a Scala data structure. This is probably the best option if you want your code to also cross-compile.

如果您只编写Scala.js代码,并且想要一个更轻量级的版本(无依赖项),建议您为JSON"schema"声明类型,然后使用这些类型以更安全的方式执行转换:

If you're only writing Scala.js code, and you want a more lightweight version (no dependency), I recommend declaring types for your JSON "schema", then use those types to perform the conversion in a safer way:

import scala.scalajs.js
import scala.scalajs.js.annotation._

// type of {"as_str":"Six","as_int":6,"is_odd":false,"factors":[1,2,3,6],"sroot":2.4494}
@ScalaJSDefined
trait Row extends js.Object {
  val as_str: String
  val as_int: Int
  val is_odd: Boolean
  val factors: js.Array[Int]
  val sroot: Double
}

type Rows = js.Array[Row]

val rows = js.JSON.parse(jsonStr3).asInstanceOf[Rows]

val items = (for (row <- rows) yield {
  import row._
  (as_str, as_int, is_odd, factors.toArray, sroot)
}).toSeq

这篇关于在Scala.js中解析数据库行的JSON表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆