I'm converting a json-like string into json, and the following code works in the scala repl

import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.JsonDSL.WithDouble._
import org.json4s.native.JsonMethods._ val value = "{100:1.50;500:1.00;1000:0.50}" val data = value.stripPrefix("{").stripSuffix("}").split(";").map(a => {
val b = a.split(":")
(b(0),b(1))
}).toMap
compact(render(data))

But when it is compiled, I'm getting the following error

[error] ... type mismatch;
[error] found : scala.collection.immutable.Map[String,String]
[error] required: org.json4s.JValue
[error] (which expands to) org.json4s.JsonAST.JValue
[error] compact(render(data))
[error] ^

Why is this, and how might I fix it?

I suspect something with the type system that is over my head.

render() is imported from JsonMethods._ and it actually requires a JValue. You have imported an implicit map2jvalue twice from those two imports import org.json4s.JsonDSL._ and import org.json4s.JsonDSL.WithDouble._.

I suspect that the compiler didn't find the implicit due to the ambiguous imports, try to be more selective: the 3rd import seems redundant (the one with JsonDSL.WithDouble._).

Sometimes you can run scalac with -Xlog-implicits to see why implicits are not used.

Why does this json4s code work in the scala repl but fail to compile?的更多相关文章

  1. Go 1 Release Notes

    Go 1 Release Notes Introduction to Go 1 Changes to the language Append Close Composite literals Goro ...

  2. Adaptive Code Via C#读书笔记

    原书链接: http://www.amazon.com/Adaptive-Code-via-principles-Developer-ebook/dp/B00OCLLYTY/ref=dp_kinw_s ...

  3. Scala on Visual Studio Code

    Download and install Scala Download a scala installation package from here. Then install it. Linux s ...

  4. How to compile and install Snort from source code on Ubuntu

    http://www.tuicool.com/articles/v6j2Ab Snort is by far the most popular open-source network intrusio ...

  5. Rewrite MSIL Code on the Fly with the .NET Framework Profiling API

    http://clrprofiler.codeplex.com/ http://blogs.msdn.com/b/davbr/archive/2012/11/19/clrprofiler-4-5-re ...

  6. 在 json4s 中自定义CustomSerializer

    到目前为止,Scala 环境下至少存在6种 Json 解析的类库,这里面不包括 Java 语言实现的 Json 类库.所有这些库都有一个非常相似的抽象语法树(AST).而 json4s 项目旨在提供一 ...

  7. 如何在 VS Code 中搭建 Qt 开发环境

    前言 VS Code 高大上的界面.强大的智能联想和庞大的插件市场,着实让人对他爱不释手.虽然可以更改 Qt Creator 的主题,但是 Qt Creator 的代码体验实在差劲.下面就来看看如何在 ...

  8. Akka-CQRS(15)- Http标准安全解决方案:OAuth2+JWT

    上期讨论过OAuth2, 是一种身份认证+资源授权使用模式.通过身份认证后发放授权凭证.用户凭授权凭证调用资源.这个凭证就是一种令牌,基本上是一段没什么意义的加密文,或者理解成密钥也可以.服务方通过这 ...

  9. [转]How to: Create a Custom Principal Identity

    本文转自:https://msdn.microsoft.com/en-us/library/aa702720(v=vs.110).aspx The PrincipalPermissionAttribu ...

随机推荐

  1. 手持终端打印POS机(安装移动销售开单订货会软件)无线传输到订货会后台销售管理系统

    当今的服装市场是品牌竞争时代,产品能否紧随潮流前线并迅速推出市场抢得先机,是品牌成功与否的关键.而订货会是每个鞋服企业新产品走向市场至关重要的开端,订货会如何演绎.成功与否,与品牌在竞争洪流中的命运息 ...

  2. [工作中的设计模式]策略模式stategy

    一.模式解析 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. 策略模式的关键点为: 1.多种算法存在 2.算法继承同样的接口 ...

  3. vector初始化

    对vector对象来说,直接初始化的方式适用于3种情况:1.初始值已知且数量少:2.初始值是另一个vector对象的副本:3.所有元素的初始值都一样.此外还有一种更常见的情况是4.创建一个vector ...

  4. 非传统题【A002】

    [A002]非传统题[难度A]————————————————————————————————————————————————————————————————————————————————————— ...

  5. Java解析HTML之HTMLParser使用与详解

    http://blog.csdn.net/jediael_lu/article/details/26285951

  6. http://blog.csdn.net/jyw935478490/article/details/51233931

    http://blog.csdn.net/jyw935478490/article/details/51233931

  7. sed 字符串替换

    1. sed替换的基本语法为: sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义. 2. 单引号” ‘ ’”是没有办法用反 ...

  8. 后缀数组 SPOJ 694 Distinct Substrings

    题目链接 题意:给定一个字符串,求不相同的子串的个数 分析:我们能知道后缀之间相同的前缀的长度,如果所有的后缀按照 suffix(sa[0]), suffix(sa[1]), suffix(sa[2] ...

  9. docker不稳定 short running containers with -rm failed to destroy

    正常运行以下命令 sudo docker run --rm busybox echo helloworld /var/log/upstart/docker.log 日志如下: // :: POST / ...

  10. enum枚举类型的使用

    修饰符为public static enum,不用加final,否则提示错误. 枚举类的所有实例必须在枚举类中显式列出(,分隔,; 结尾).列出的实例系统会自动添加 public static fin ...