{"id":16964965,"url":"https://github.com/propensive/jacinta","last_synced_at":"2025-03-22T14:31:00.113Z","repository":{"id":37234964,"uuid":"223797857","full_name":"propensive/jacinta","owner":"propensive","description":"Simple interfaces for reading, processing and writing JSON in Scala","archived":false,"fork":false,"pushed_at":"2025-02-11T23:26:55.000Z","size":3353,"stargazers_count":6,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T23:34:40.449Z","etag":null,"topics":["ast","json","json-api","scala","serializer"],"latest_commit_sha":null,"homepage":"https://propensive.com/jacinta/","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/propensive.png","metadata":{"files":{"readme":".github/readme.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-24T19:26:27.000Z","updated_at":"2025-02-11T23:26:58.000Z","dependencies_parsed_at":"2023-11-16T09:25:58.706Z","dependency_job_id":"9c009cfd-662b-42cc-82c3-920778bc6574","html_url":"https://github.com/propensive/jacinta","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fjacinta","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fjacinta/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fjacinta/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fjacinta/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/propensive","download_url":"https://codeload.github.com/propensive/jacinta/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244971774,"owners_count":20540853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","json","json-api","scala","serializer"],"created_at":"2024-10-13T23:44:42.558Z","updated_at":"2025-03-22T14:31:00.106Z","avatar_url":"https://github.com/propensive.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"[\u003cimg alt=\"GitHub Workflow\" src=\"https://img.shields.io/github/actions/workflow/status/propensive/jacinta/main.yml?style=for-the-badge\" height=\"24\"\u003e](https://github.com/propensive/jacinta/actions)\n[\u003cimg src=\"https://img.shields.io/discord/633198088311537684?color=8899f7\u0026label=DISCORD\u0026style=for-the-badge\" height=\"24\"\u003e](https://discord.com/invite/MBUrkTgMnA)\n\u003cimg src=\"/doc/images/github.png\" valign=\"middle\"\u003e\n\n# Jacinta\n\n__Simple interfaces for reading, processing and writing JSON__\n\n_Jacinta_ is a fully-featured JSON library for Scala, built upon the JSON parser,\n[Merino](https://github.com/propensive/merino/), and designed to make it easy\nand safe to work with JSON in Scala.\n\n## Features\n\n- parse and serialize JSON\n- intuitive dynamic API for quick field access, without compromising typesafety\n- automatic conversion to and from product and sum types\n\n\n## Availability\n\n\n\n\n\n\n\n## Getting Started\n\nAll Jacinta terms and types are in the `jacinta` package, and exported to the `soundness` package.\nSo we begin either by importing,\n```scala\nimport jacinta.*\n```\nor:\n```scala\nimport soundness.*\n```\n\n### Core types\n\nJacinta's most important type is `Json` which represents an instance of a JSON value, that is,\na JSON object, array or primitive (string, number or boolean). It does not represent\n_serialized JSON_, so details like whitespace and the ordering of keys in an object are not\nrepresented.\n\nScala's type system knows nothing more about the internal structure of a `Json` value than this. So\na `Json` value representing the number `12` is indistinguishable by the type system from a `Json`\nvalue representing an array of complex objects.\n\n### Parsing\n\nWe can obtain a `Json` value by constructing one from existing values.. Or we can parse some textual\ninput.\n\nThe `Json.parse` method takes any input that is `Readable by Bytes`. This includes not only\n`Text` and `Bytes` values, but other types like filesystem `Path`s—if suitable context is provided\nfor a `Readable by Bytes` value to be resolved.\n\nHere is an example of parsing `Text` as JSON:\n```scala\nJson.parse(t\"\"\"{ \"name\": \"Alfred\", \"age\": 83 }\"\"\")\n```\n\nCalling `Json.parse` may raise a `JsonParseError`, so this should be handled in some way. Full\ndetails of error handling in Soundness is provided by\n[Contingency](https://github.com/propensive/contingency/).\n\n### Dynamic field access\n\nAs a dynamically-typed value, Scala's type system does not know anything about the fields that are\navailable on a particular `Json` value. It does not even know if it is an object with fields, an\narray with indices, or a primitive value.\n\nBut we may know more than the type system. Or at least, we may wish to program to the assumption\nthat we know more. So Jacinta makes it possible to access fields and array indices _dynamically_\nusing selection or arbitrary field names with the familiar `.`, and application with parentheses\nfor numeric indices.\n\nThis would normally be a significant compromise on typesafety, since it would allow us to call\nnonexistent methods on `Json` values, without protection from the compiler. So access must be\nexplicitly enabled with the import:\n```scala\nimport dynamicJsonAccess.enabled\n```\n\nWith this contextual value in-scope, we can dereference and deindex `Json` values, dynamically.\nThe result will always be another `Json` value, ready to be deindexed or dereferenced, or a\n`JsonError` will be thrown if the index is out of range, or the object key does not exist.\n\n`Json` values are not useful (in most cases) for use elsewhere in Scala code, unless we can\nconvert them to typed Scala values. This is called _decoding_.\n\n### Decoding `Json` values\n\n\n\n### Encoding as `Json`\n\nMany Scala values can be mapped directly (and often unambiguously) to JSON values. Trivially, this\nincludes `Text`, `Boolean` and various numeric types. But collection types like `List` and `Set`\ncan also be mapped to JSON arrays, if their elements are types which can be mapped. And case\nclasses and tuples of these types may also be mapped, so long as their elements can. With a suitable\nchoice of encoding, sum types (like enumerations or sealed traits) can also be mapped.\n\nThis is true compositionally. For example, `List`s of sealed traits, composed of case classes whose\nparameters are tuples of `Int`s, `Set`s and other case classes are equally encodable.\n\nEncoding a value to `Json` is as simple as calling `.json` on that value. If it is able to be\nencoded, the result will be a `Json` value. (Note that this is not the same as encoding a `Json`\nvalue to string-like representation, which is a useful—but different—operation, described below.)\n\nHere is an example of a simple, but nontrivial case-class structure:\n```scala\ncase class Person(firstName: Text, lastName: Text)\ncase class Recipient(person: Person, emailAddress: Text)\n\nval recipient = Recipient(Person(t\"Piotr\", t\"Nowak\"), t\"pn@example.com\")\n```\n\nGiven these definitions, the `recipient` instance can be encoded with `recipient.json` into a\n`Json` value representing the following JSON object:\n```json\n{\n  \"person\": {\n    \"firstName\": \"Piotr\",\n    \"lastName\": \"Nowak\"\n  },\n  \"emailAddress\": \"pn@example.com\"\n}\n```\n\n#### Coproducts\n\n\n### Encoding as `Text`\n\n\n### Decoding as `Json`\n\n\n\nSealed traits of two or more case class subtypes will be serialized to JSON objects, exactly as each of the\nsubtypes would be, but with an additional field called `_type`, whose value will be set to the unqualified type\nname, e.g. `\"_type\": \"Leaf\"`.\n\nAlthough this encoding of coproduct types is non-standard, it is a reasonable default, and can always be\noverridden with specific typeclass instances for the sealed trait type.\n\n#### Collections\n\nFurthermore, all traversable standard collection types can be serialized to JSON arrays, provided the elements\nof the collection can be.\n\n### Acessing values\n\nInstances of `Json` are dynamically-typed which means that members with arbitrary names may be accessed as if\nthey were methods. Taking the `recipient` example above, it would be valid to access `recipient.person`, as if\nthe method `person` existed on the `Json` type. It doesn't, but since `Json` instances inherit from the special\n`Dynamic` trait, the code will be transformed into `recipient.selectDynamic(\"person\")` at compiletime, which\nwill return a new `Json` instance representing the JSON:\n```json\n{\n  \"firstName\": \"Mike\",\n  \"lastName\": \"Smith\"\n}\n```\n\nIt is therefore possible to call `recipient.person.firstName` directly and get a `Json` value representing the\nJSON string, `\"Mike\"`.\n\nAs dynamic values with little known statically about them, instances of type `Json` are not particularly useful\n_directly_, and should be converted to other types like `Text`, `Int` or `Person` before being used elsewhere in\na program. This is achieved with the `Json#as` method which takes the destination type as a parameter, for\nexample,\n```scala\nval addressee: Text = recipient.person.firstName.as[Text]\n```\nor,\n```scala\nval person: Person = recipient.person.as[Person]\n```\n\nAs well as accessing arbitrary fields in a JSON object, elements of an array may be accessed by simply applying\nthe integer index to a `Json` value representing an array, for example, `json.organisation.users(2).as[User]`.\n\n#### Errors\n\nSince dynamic field access is unchecked at compiletime, it's possible that a JSON object would not contain the\nrequested field, or a JSON array would not contain the requsted index. This will throw an exception only when\nattempting to convert the value to a static type. So the expression, `recipient.user.firstName`, (noting that\n`user` is not a valid field of `recipient`) would not produce an error in itself. Only when invoking,\n`recipient.user.firstname.as[Text]` would an exception be thrown, of type `JsonAccessError`.\n\nSimilarly, if the expression, `recipient.person.firstName.as[Int]` were evaluated, a `JsonAccessError` would be\nthrown due to the field, `firstName` being a JSON string and not a JSON number.\n\nAll methods which throw exceptions are annotated with `throws` clauses, and if `saferExceptions` is enabled,\nthese must be handled.\n\n### Typeclasses\n\nWhile all Java primitive types and `String`s, collection types and case class types can be serialized and\ndeserialized automatically, it's possible to support other types or to replace existing default implementations\nby providing contextual instances of the typeclasses, `Json.Writer` and `Json.Reader`.\n\nFor example, assuming the existence of an `Email` type (which simply wraps a `Text` instance), a `Reader` and\n`Writer` for `Email` could be provided in `Email`'s companion object, like so:\n```scala\ncase class Email(value: Text)\n\nobject Email:\n  given Json.Reader[Email] = json =\u003e Email(json.as[Text])\n  given Json.Writer[Email] = _.value.json\n```\nNote that `Email` is a case class, so default instances of `Json.Reader[Email]` and `Json.Writer[Email]` would\nexist already, but would be replaced by these new definitions. (If `Email` were instead a non-`case` `class`,\nthese would be chosen unambiguously as the only contextual instances.)\n\n#### Functor and Cofunctor\n\n`Json.Reader`s are functors, and the `Reader#map` method is provided to transform a reader of one type into a\nreader of another. Likewise, `Json.Writer`s are cofunctors with `Writer#contramap` methods. Given these\ndefinitions, an alternative way to write the definitions for `Email` by transforming the existing instances for\nthe `Text` type would be:\n```scala\nobject Email:\n  given Json.Reader[Email] = summon[Json.Reader[Text]].map(Email(_))\n  given Json.Writer[Email] = summon[Json.Writer[Text]].contramap(_.value)\n```\n\n\n## Status\n\nJacinta is classified as __fledgling__. For reference, Soundness projects are\ncategorized into one of the following five stability levels:\n\n- _embryonic_: for experimental or demonstrative purposes only, without any guarantees of longevity\n- _fledgling_: of proven utility, seeking contributions, but liable to significant redesigns\n- _maturescent_: major design decisions broady settled, seeking probatory adoption and refinement\n- _dependable_: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version `1.0.0` or later\n- _adamantine_: proven, reliable and production-ready, with no further breaking changes ever anticipated\n\nProjects at any stability level, even _embryonic_ projects, can still be used,\nas long as caution is taken to avoid a mismatch between the project's stability\nlevel and the required stability and maintainability of your own project.\n\nJacinta is designed to be _small_. Its entire source code currently consists\nof 618 lines of code.\n\n## Building\n\nJacinta will ultimately be built by Fury, when it is published. In the\nmeantime, two possibilities are offered, however they are acknowledged to be\nfragile, inadequately tested, and unsuitable for anything more than\nexperimentation. They are provided only for the necessity of providing _some_\nanswer to the question, \"how can I try Jacinta?\".\n\n1. *Copy the sources into your own project*\n   \n   Read the `fury` file in the repository root to understand Jacinta's build\n   structure, dependencies and source location; the file format should be short\n   and quite intuitive. Copy the sources into a source directory in your own\n   project, then repeat (recursively) for each of the dependencies.\n\n   The sources are compiled against the latest nightly release of Scala 3.\n   There should be no problem to compile the project together with all of its\n   dependencies in a single compilation.\n\n2. *Build with [Wrath](https://github.com/propensive/wrath/)*\n\n   Wrath is a bootstrapping script for building Jacinta and other projects in\n   the absence of a fully-featured build tool. It is designed to read the `fury`\n   file in the project directory, and produce a collection of JAR files which can\n   be added to a classpath, by compiling the project and all of its dependencies,\n   including the Scala compiler itself.\n   \n   Download the latest version of\n   [`wrath`](https://github.com/propensive/wrath/releases/latest), make it\n   executable, and add it to your path, for example by copying it to\n   `/usr/local/bin/`.\n\n   Clone this repository inside an empty directory, so that the build can\n   safely make clones of repositories it depends on as _peers_ of `jacinta`.\n   Run `wrath -F` in the repository root. This will download and compile the\n   latest version of Scala, as well as all of Jacinta's dependencies.\n\n   If the build was successful, the compiled JAR files can be found in the\n   `.wrath/dist` directory.\n\n## Contributing\n\nContributors to Jacinta are welcome and encouraged. New contributors may like\nto look for issues marked\n[beginner](https://github.com/propensive/jacinta/labels/beginner).\n\nWe suggest that all contributors read the [Contributing\nGuide](/contributing.md) to make the process of contributing to Jacinta\neasier.\n\nPlease __do not__ contact project maintainers privately with questions unless\nthere is a good reason to keep them private. While it can be tempting to\nrepsond to such questions, private answers cannot be shared with a wider\naudience, and it can result in duplication of effort.\n\n## Author\n\nJacinta was designed and developed by Jon Pretty, and commercial support and\ntraining on all aspects of Scala 3 is available from [Propensive\nO\u0026Uuml;](https://propensive.com/).\n\n\n\n## Name\n\n_Jacinta_ is one of the feminine forms of the given name Jason, which is homophonous to JSON.\n\nIn general, Soundness project names are always chosen with some rationale,\nhowever it is usually frivolous. Each name is chosen for more for its\n_uniqueness_ and _intrigue_ than its concision or catchiness, and there is no\nbias towards names with positive or \"nice\" meanings—since many of the libraries\nperform some quite unpleasant tasks.\n\nNames should be English words, though many are obscure or archaic, and it\nshould be noted how willingly English adopts foreign words. Names are generally\nof Greek or Latin origin, and have often arrived in English via a romance\nlanguage.\n\n## Logo\n\nThe logo shows a pair of braces—important syntax in JSON—positioned to look like two people face-to-face, alluding to the concept of _communication_.\n\n## License\n\nJacinta is copyright \u0026copy; 2025 Jon Pretty \u0026 Propensive O\u0026Uuml;, and\nis made available under the [Apache 2.0 License](/license.md).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fjacinta","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpropensive%2Fjacinta","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fjacinta/lists"}