使用Scala进行无噪音的JSON处理 [英] Noise-free JSON processing with Scala

查看:71
本文介绍了使用Scala进行无噪音的JSON处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自dotnet,但是最近一直在寻找替代编程语言的可能性.没什么真正的严重的,只是到处都是.最近,我发现了Scala,并且对它非常着迷.尽管没有确定性,但我对C#中对我来说很重要的内容进行了一些中间检查,我感到相当满意:功能概念-刻度,临时多态性-刻度,注释-刻度,反射和代码生成-刻度.

I'm coming from a dotnet land, but recently have been looking at the possibilities of alternative programming languages. Nothing really serious, just some bits here and there. Recently I've discovered Scala and I'm pretty fascinated with it. Despite non-deterministic tinkering, I've done some intermediate checks of stuff that is important for me in C# and I feel rather satisfied: functional notions - tick, ad-hoc polymorphism - tick, annotations - tick, reflection and codegen - tick.

现在,我正在考虑如何使用DLR和动态"语法糖来编程在C#4.0中实现的JSON处理库的类似物.这是我要寻找的功能集:

Now I'm thinking about how one would program an analogue of JSON processing library I've implemented in C# 4.0 with the help of DLR and "dynamic" syntactic sugar. Here's the feature set I'm looking for:

  1. 方便地浏览和构建原始JSON.
  2. 在JSON与本机对象/集合之间进行自动转换(一般情况下,这个问题是无法解决的,尽管可以定义在95%的时间内都可以使用的约定-这对我来说很好).

这里有C#4.0的新功能,因为它们使我可以覆盖成员访问并进行类型转换以执行完全自定义的逻辑(如果C#4.0中的变量被键入为动态",则您将对其进行的所有操作都将被编译调用具有合理默认行为的程序员定义的方法-请参见 DynamicMetaObject. MSDN上的BindXXX方法以获取更多信息).例如.我已经重写了类型强制转换以对.NET对象和成员访问进行序列化/反序列化以管理原始JSON,以便可以编写以下代码:

New features of C# 4.0 kinda rock here, since they let me override member access and type casts to perform completely custom logic (if a variable in C# 4.0 is typed as "dynamic", then anything you do with it will be compiled into calls to programmer-defined methods with reasonable default behaviour - see DynamicMetaObject.BindXXX methods at MSDN for more info). E.g. I've overriden type casts to serialize/deserialize .NET objects and member accesses to manage raw JSON, so that I can write the following code:

var json = Json.Get("http://some.service");
if (json.foo) Console.WriteLine((Foo)json.foo);
json.bars = ((List<Bar>)json.bars).DoSomething();

当然,这不是理想的,因为C#4.0中的动态绑定在扩展方法和类型推断方面存在问题,此外,代码仍然感觉很重.但是无论如何,这比使用我在c#3.5中曾经使用过的所有((JsonObject)json ["quux"])["baz"]都要好.

Of course, this is not ideal, since dynamic binding in C# 4.0 has problems with extension methods and type inference, and, moreover, the code still feels rather heavyweight. But anyways, this is much better than using all those ((JsonObject)json["quux"])["baz"] I've used to in c# 3.5.

一些基础研究表明,Scala没有支持后期绑定的专用语言功能.但是,有很多技巧,也许可以一起使用它们来创建上面显示的代码的可忍受的仿真(甚至更好,我几乎可以肯定这是可能的).你能在这里给我一些建议吗?

Some basic research shows that Scala doesn't have dedicated language features that support late binding. However, there are so many tricks that maybe they can be used together to create a bearable emulation of the code shown above (or even to be better - I almost sure that this is possible). Could you, please, advise me something here?

推荐答案

Scala有用的JSON库是lift-json,它是Lift Web Framework的独立组件.

A useful JSON library for Scala is lift-json, which is a standalone component of the Lift Web Framework.

https://github.com/lift/framework/tree/master/核心/json

它支持提取类,解析和用于创建JSON的DSL.

It supports extraction to classes, parsing and a DSL for creating JSON.

我链接到的页面上有完整的教程,所以我不会仅仅复制和粘贴它.

The page I linked to has a comprehensive tutorial, so I won't just copy and paste it.

这篇关于使用Scala进行无噪音的JSON处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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