Writing a Case Class for Json in Scala
json-schema-to-case-class
1.2.0 • Public • Published
- Readme
- Explore BETA
- 3 Dependencies
- 0 Dependents
- 9 Versions
A library to convert complex JSON Schema to Scala Case Classes. Supports both NodeJs and Browser environments. For NodeJs (TypeScript): For NodeJs (JavaScript): For browser: If you are using the prebuild bundle, it has all the APIs under It is optional to pass configuration object. Every configuration setting is optional as well. When not passed, default kicks-in. This library supports recent versions of every major web browsers. Refer to the browserified build As of now, it does not support a few latest JSON Schema features such as All contributions, enhancements, and bug-fixes are welcome. Open an issue or create a pull request. Clone the repo Install Dependancies Setup development environment Run the test Run eslint checks Run eslint checks and fix the errors json-schema-to-case-class
Try Online Editor.
JSON Schema Generated Scala Case Classes { "title": "Product", "type": "object", "required": [ "productId", "productName", "price" ], "properties": { "productId": { "type": "integer" }, "productName": { "type": "string" }, "price": { "type": "number" }, "tags": { "type": "array", "items": { "type": "string" } }, "dimensions": { "type": "object", "properties": { "length": { "type": "number" }, "width": { "type": "number" }, "height": { "type": "number" } } } } }
case class Product ( productId: Integer, productName: String, price: Double, tags: Option[List[String]], dimensions: Option[Dimensions] )
case class Dimensions ( length: Double, width: Double, height: Double )
Features
Option[]
.description
provided in JSON Schema.Any
)Enumeration
objects for properties with enum
field. Installing
npm install --save json-schema-to-case-class
Usage
import { convert , IConfig } from 'json-schema-to-case-class' const mySchema: any = { ... } ; const config: IConfig = { ... } ; // <-- Optional // With default configuration: convert ( mySChema ) . then ( result => console . log ( result ) ) . catch ( err => console . error ( err ) ) // With custom configuration: convert ( mySchema , config ) . then ( result => console . log ( result ) ) . catch ( err => console . error ( err ) )
const { convert } = require ( 'json-schema-to-case-class' ) ; // OR const SchemaConverter = require ( 'json-schema-to-case-class' ) ; SchemaConverter . convert ( , ) ;
SchemaConverter
global object: SchemaConverter . convert ( mySchema , config )
Configuration
IConfig
:
Name Type Description Default maxDepth number Parses nested schema objects upto the depth = maxDepth. Pass 0 for no limit on depth. 0 optionSetting string Setting to wrap optional parameters in Option[]
.
1. "useOptions" - Wrap optional parameters in Option[]
2. "useOptionsForAll" - Wrapp all parameters in Option[]
3. "noOptions" - Don't wrap any parameter in Option[]
useOptions classNameTextCase string Text case for case class title. Could be one of:
camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCasepascalCase classParamsTextCase string Text case for case class parameters. Could be one of above. snakeCase topLevelCaseClassName string This config kicks-in only if top level schema object does not have 'title' property. MyCaseClass defaultGenericType string Case class parameter type when type
information not available from the schema or if we are converting partial schema using maxDepth
setting.Any parseRefs boolean Whether to resolve the local or remote schema references ($ref). true generateComments boolean Whether to generate scaladoc-like comments for the case class generated. false generateValidations boolean Whether to generate validations in the form of assertions in case class body. false generateEnumerations boolean Whether to generate enumerations for enum
fields. It generates an object extending scala's Enumeration
class and use it in parameter type.false Browser Support
dist/js2cc.min.js
that you can directly use in <script />
tag of HTML page. It already bundles all the required polyfills. Limitations
allOf
/ anyOf
/ oneOf
. Contributing
Short-term Goals
allOf
/ anyOf
/ oneOf
.exclusiveMinimum
, minItems
, uniqueItems
. Building locally
https://github.com/cchandurkar/json-schema-to-case-class.git
npm install
npm run setup-dev-env
npm test
npm run lint
npm run lint:fix
Writing a Case Class for Json in Scala
Source: https://www.npmjs.com/package/json-schema-to-case-class
0 Response to "Writing a Case Class for Json in Scala"
Post a Comment