Scala案例类到json模式

时间:2015-06-25 15:03:53

标签: json scala rest

我想生成案例类的json架构,以便向其他服务提供一些信息,这些服务将使用rest api公开我的应用程序

我上过这堂课:

case class Vendor(
                  name: String,
                  synonyms: List[String],
                  transalit: String,
                  urlPart: String)

我怎样才能这样生成:

{
   "type":"object",
   "properties":{
      "name":{
         "type":"string"
      },
      "synonyms":{
         "type":"array",
         "items":{
            "type":"string"
         }
      },
      "translit":{
         "type":"string"
      },
      "urlPart":{
         "type":"string"
      }
   }
}

我发现了这个:https://github.com/coursera/autoschema但是sbt找不到依赖。

我也找到了这个Is there a way to get a JSON-Schema from a Scala Case Class hierarchy?,这个问题与我的问题非常相似,但没有答案..

我可能正在寻找不存在的答案。使用其他技术可能会更好

2 个答案:

答案 0 :(得分:6)

似乎autoschema的Maven工件不存在,这就是sbt无法找到依赖关系的原因。

好消息是,使用sbt,您可以从github导入项目并将其添加为依赖项。在build.sbt添加以下内容:

lazy val autoschemaProject =
  ProjectRef(uri("https://github.com/coursera/autoschema.git"), "autoschema")

lazy val root = (project in file(".")).dependsOn(autoschemaProject)

请注意,root中可能已经定义了build.sbt,在这种情况下只会添加dependsOn(autoschemaProject)。 我使用sbt 0.13.7进行了测试,并设法使用autoschema从案例类生成json模式。

答案 1 :(得分:0)

该项目已分叉: https://github.com/sauldhernandez/autoschema

现在,您可以通过将此依赖项添加到build.sbt中来获得工件:

libraryDependencies += "com.sauldhernandez" %% "autoschema" % "1.0.4"