Code Monkey home page Code Monkey logo

Comments (2)

Doikor avatar Doikor commented on July 29, 2024 1

Made a simple example repo showing this.

https://github.com/Doikor/jsonapi-scala-example

I will update the readme on this repo also.

from jsonapi-scala.

Doikor avatar Doikor commented on July 29, 2024

You need to add macro paradise plugin for it to work. Another thing I've forgotten to mention in the readme I guess :(

When using the console from the project (so the macro paradise is added) it works

sbt:jsonapi-scala> ++2.12.3
[info] Setting Scala version to 2.12.3 on 3 projects.
[info] Excluded 2 projects, run ++ 2.12.3 -v for more details.
[info] Reapplying settings...
[info] Set current project to jsonapi-scala (in build file:/Users/ahuttunen/code/jsonapi-scala/)
sbt:jsonapi-scala> core/console

... bunch of stuff compiling

Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

import _root_.spray.json.DefaultJsonProtocol._
import com.qvantel.jsonapi._

implicit val apiRoot: ApiRoot = ApiRoot(None)

@jsonApiResource final case class Employee(id: String, name: String)
@jsonApiResource final case class Company(id: String, name: String, employees: ToMany[Employee])

val acme = Company("1", "acme", ToMany.loaded(Seq(Employee("1", "number one 1"))))

val json = rawOne(acme)
val parsed = readOne[Company](json, Set("employees"))

acme == parsed // true

// Exiting paste mode, now interpreting.

import _root_.spray.json.DefaultJsonProtocol._
import com.qvantel.jsonapi._
apiRoot: com.qvantel.jsonapi.ApiRoot = ApiRoot(None)
defined class Employee
defined object Employee
defined class Company
defined object Company
acme: Company = Company(1,acme,Loaded(List(Employee(1,number one 1))))
json: spray.json.JsObject = {"data":{"attributes":{"name":"acme"},"relationships":{"employees":{"data":[{"type":"employees","id":"1"}],"links":{"related":"/companies/1/employees"}}},"links":{"self":"/companies/1"},"id":"1","type":"companies"},"included":[{"type":"employees","attributes":{"name":"number one 1"},"id":"1","links":{"self":"/employees/1"}}]}
parsed: Company = Company(1,acme,Loaded(List(...

So try adding this to your build.sbt

resolvers += Resolver.sonatypeRepo("releases")
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

from jsonapi-scala.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.