Skip to content

Latest commit

 

History

History
108 lines (82 loc) · 3.92 KB

USERGUIDE.md

File metadata and controls

108 lines (82 loc) · 3.92 KB

Maven Central coverage
Gitter Matrix

Integrations

<script type='module' src="https://cdn.jsdelivr.net/gh/nrktkt/sauce@11.1.0/sauce.js">

If you're reading this, you should go to the userguide website at https://nrktkt.github.io/ninny-json/USERGUIDE

</script>

Reading values from JSON

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/Reading.scala' lines='8:63'

Writing values to JSON

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/Writing.scala' lines='9:23'

obj and arr build JSON structures

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/Writing.scala' lines='25:47'

note: The above example uses magnets to produce an object building syntax similar to how you'd build a scala.collection.Map. This is intuitive, but can produce unhelpful compiler errors. Instead, you can use a "long arrow" --> or "squiggly arrow" ~> to get better compiler errors for debugging, or just because you don't like magnets. See below for an example

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/Writing.scala' lines='52:74'

Updating nested values

With immutable ASTs it can be a pain to update values deep inside the tree.
You can use ninny's dynamic update syntax easly to replace values way down in there.

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/Updating.scala' lines='5:18'

Converting domain objects to JSON

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/DomainTo.scala' lines='7:51'

Converting JSON to domain objects

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/DomainFrom.scala' lines='22:49'

Rather than implementing ToJson and FromJson by hand, you can generate them automatically using

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/SemiAuto.scala' lines='11:16'

See more on configuring automatic derivation here.

forProductN derivation for ToJson and FromJson

Often you want to have different names in a case class than what's in the JSON, but you don't need to change the types at all. forProductN methods are perfect for this, just specify what the JSON field name should be and provide the case class apply/unapply method to generate a ToJson, FromJson, or both together.

<sauce-code repo='nrktkt/ninny-json' lang='scala' file='ninny/test/src-2/nrktkt/ninny/userguide/ForProductN.scala' lines='8:24'