{"id":20406819,"url":"https://github.com/wkgcass/vjson","last_synced_at":"2025-04-12T15:08:29.526Z","repository":{"id":57736421,"uuid":"202796825","full_name":"wkgcass/vjson","owner":"wkgcass","description":"A simple JSON parser and (de)serializer library for java/kotlin and kotlin-native/js. Also provides a strongly-typed language: vjson-script.","archived":false,"fork":false,"pushed_at":"2023-12-22T14:02:31.000Z","size":4364,"stargazers_count":48,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-12T15:08:16.193Z","etag":null,"topics":["json","kotlin-js","kotlin-native"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wkgcass.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-08-16T20:43:38.000Z","updated_at":"2024-08-09T09:53:37.000Z","dependencies_parsed_at":"2023-11-20T17:29:48.931Z","dependency_job_id":"1d2ef833-6021-4163-a83b-0f16680e60b6","html_url":"https://github.com/wkgcass/vjson","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wkgcass%2Fvjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wkgcass%2Fvjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wkgcass%2Fvjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wkgcass%2Fvjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wkgcass","download_url":"https://codeload.github.com/wkgcass/vjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586235,"owners_count":21128997,"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":["json","kotlin-js","kotlin-native"],"created_at":"2024-11-15T05:19:28.468Z","updated_at":"2025-04-12T15:08:29.507Z","avatar_url":"https://github.com/wkgcass.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vjson\n\n[中文](https://github.com/wkgcass/vjson/blob/master/README_ZH.md) | English\n\n## intro\n\nvjson is a light weight json parser/deserializer/builder lib built for java/kotlin and kotlin native.\n\nThe lib focuses on providing the original json representation in java object form, and letting you build any json string using only java method invocations, or deserialize into java objects without reflection, which is a great feature for building `native-image`s.\n\n## performance\n\nCheck `src/test/java/vjson/bench` for more info. You may run the jmh tests or a simple test directly with the main method.\n\n## reliability\n\n`vjson` has 100% line + branch coverage.\n\nRun `src/test/java/vjson/Suite.java` to test the lib.\n\nRun `./gradlew clean coverage` to get the coverage report.\n\n## use\n\n### if you do no use kotlin\n\n**gradle**\n\n```groovy\n// with module-info (also works for jdk8)\nimplementation 'io.vproxy:vjson:1.5.5-jdk9'\n/* without module-info (if your toolchain doesn't work, use this version instead)\nimplementation 'io.vproxy:vjson:1.5.5'\n*/\n```\n\n**maven**\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.vproxy\u003c/groupId\u003e\n  \u003cartifactId\u003evjson\u003c/artifactId\u003e\n\u003c!-- with module-info (also works for jdk8) --\u003e\n  \u003cversion\u003e1.5.5-jdk9\u003c/version\u003e\n\u003c!-- without module-info (if your toolchain doesn't work, use this version instead)\n  \u003cversion\u003e1.5.5\u003c/version\u003e\n--\u003e\n\u003c/dependency\u003e\n```\n\n### if you are using kotlin\n\n**gradle**\n\n```groovy\nimplementation('io.vproxy:vjson:${vjsonVersion}') {\n  exclude group: 'io.vproxy', module: 'kotlin-stdlib-lite'\n}\n```\n\n**maven**\n\n```xml\n\u003cdependencyManagement\u003e\n  \u003cdependencies\u003e\n    \u003cdependency\u003e\n      \u003cgroupId\u003eio.vproxy\u003c/groupId\u003e\n      \u003cartifactId\u003evjson\u003c/artifactId\u003e\n      \u003cversion\u003e${vjson.version}\u003c/version\u003e\n      \u003cexclusions\u003e\n        \u003cexclusion\u003e\n          \u003cgroupId\u003eio.vproxy\u003c/groupId\u003e\n          \u003cartifactId\u003ekotlin-stdlib-lite\u003c/artifactId\u003e\n        \u003c/exclusion\u003e\n      \u003c/exclusions\u003e\n    \u003c/dependency\u003e\n  \u003c/dependencies\u003e\n\u003c/dependencyManagement\u003e\n```\n\n## kotlin native\n\n`vjson` is purely written in kotlin and only rely on kotlin stdlib, as a result it can be ported to kotlin native.\n\nRun `./gradlew clean kotlinNative` to compile the source code to kotlin native version.\n\nAlso you can run the following commands to build a native executable:\n\n```shell\n./gradlew clean kotlinNative\nrm -r misc/vjson_executable/src/nativeMain/kotlin/vjson/*\ncp -r src/main/kotlin/vjson/* misc/vjson_executable/src/nativeMain/kotlin/vjson\n\n# Then you can build kotlin native executable\ncd misc/vjson_executable/\n./gradlew clean nativeBinaries\n```\n\nRun with:\n\n```shell\n./build/bin/native/releaseExecutable/vjson_executable.kexe --help\n```\n\n## kotlin js\n\nKotlin JS has more restrictions than kotlin native, a standalone task is provided for kotlin js:\n\nRun `./gradlew clean kotlinJs` to compile the source code to kotlin js version.\n\nAlso you can run the following commands to update the web interpreter code:\n\n```shell\n./gradlew clean kotlinJs\nrm -r misc/online_vjson_script_interpreter/src/main/kotlin/vjson/*\ncp -r src/main/kotlin/vjson/* misc/online_vjson_script_interpreter/src/main/kotlin/vjson\n\n# Then you can run the interpreter:\ncd misc/online_vjson_script_interpreter/\n./gradlew clean run\n```\n\n## vpreprocessor\n\nThe same source code would be used on both jvm and kotlin native. Some jvm specific annotations and jdk classes are used to get better java interoperability. However they cannot be used when building kotlin native applications. To solve this problem, I developed a code preprocessor program which allow you to integrate `macro` into java/kotlin code using comments.\n\nSee [vpreprocessor/README.md](https://github.com/wkgcass/vjson/blob/master/src/main/kotlin/vpreprocessor/README.md) for more info.\n\nAlso note that the preprocessing directly rewrites source codes, so the building process requires you to keep git directory clean before doing preprocessing. A task `checkGit` is automatically invoked before `coverage`, `kotlinNative` and `kotlinJs`.\n\n## example\n\n**java**\n\n```java\n// basic\nJSON.Instance result = JSON.parse(\"{\\\"hello\\\":\\\"world\\\"}\");\nString json = result.stringify();\nString prettyJson = result.pretty();\n\n// deserialize\nRule\u003cShop\u003e shopRule = new ObjectRule\u003c\u003e(Shop::new)\n    .put(\"name\", Shop::setName, StringRule.get())\n    .put(\"goods\", Shop::setGoods, new ArrayRule\u003c\u003e(\n        ArrayList::new,\n        ArrayList::add,\n        new ObjectRule\u003c\u003e(Good::new)\n            .put(\"id\", Good::setId, StringRule.get())\n            .put(\"name\", Good::setName, StringRule.get())\n            .put(\"price\", Good::setPrice, DoubleRule.get())\n    ));\nShop shop = JSON.deserialize(jsonStr, shopRule);\n\n// autotype\nObjectRule\u003cGood\u003e goodRule = new ObjectRule\u003c\u003e(Good::new)\n    .put(\"id\", Good::setId, StringRule.get())\n    .put(\"name\", Good::setName, StringRule.get())\n    .put(\"price\", Good::setPrice, DoubleRule.get());\nObjectRule\u003cSpecialPriceGood\u003e specialPriceGoodRule = new ObjectRule\u003c\u003e(SpecialPriceGood::new, goodRule)\n    .put(\"originalPrice\", SpecialPriceGood::setOriginalPrice, DoubleRule.get());\nTypeRule\u003cGood\u003e typeGoodRule = new TypeRule\u003c\u003e(Good.class, goodRule)\n    .type(\"special\", specialPriceGoodRule);\n    // or .type(SpecialPriceGood.class, specialPriceGoodRule);\n// the TypeRule\u003c\u003e can also be constructed without arguments\n// deserializes json like: {\"@type\": \"...\", ...}\n\n// retrieve\njavaObject = result.toJavaObject(); // List,Map,String or primitive boxing types\njavaObject = JSON.parseToJavaObject(\"{\\\"hello\\\":\\\"world\\\"}\");\nString value = ((JSON.Object) result).getString(\"hello\"); // world\n\n// parse\nObjectParser parser = new ObjectParser();\nparser.feed(\"{\\\"hel\");    // return null here\nparser.feed(\"lo\\\":\\\"wo\"); // return null here\nparser.feed(\"rld\\\"}\");    // return JSON.Object here\n                          // you may use CharStream instead of String to feed the parser\n    // if it's the last piece to feed the parser:\n    parser.last(\"rld\\\"}\");\n\n// construct complex objects\nnew ObjectBuilder().put(\"id\", 1).put(\"name\", \"pizza\").build(); // JSON.Object\nnew ArrayBuilder().add(3.14).addObject(o -\u003e ...).addArray(a -\u003e ...).build(); // JSON.Array\n\n// serialize\nnew SimpleInteger(1).stringify(); // 1\nnew SimpleString(\"hello\\nworld\").stringify(); // \"hello\\nworld\"\n\n// advanced\nnew ObjectParser(new ParserOptions().setListener(...)); // hook points that the parsers will call\nresult.stringify(stringBuilder, stringifier); // customize the output format\n\n// additional features\nnew ParserOptions().setStringSingleQuotes(true).setKeyNoQuotes(true);\n                        // allow to parse strings like: {key: 'value'}\n```\n\n**kotlin**\n\n```kotlin\n// builder example with kotlin syntax\nval shopRule = ObjectRule.builder(::ShopBuilder, { build() }) {\n  put(\"name\", StringRule) { name = it }\n  put(\"goods\", { goods = it }) {\n    ArrayRule.builder({ ArrayList\u003cGood\u003e() }, { Collections.unmodifiableList(this) }, { add(it) }) {\n      ObjectRule.builder(::GoodBuilder, { build() }) {\n        put(\"id\", StringRule) { id = it }\n        put(\"name\", StringRule) { name = it }\n        put(\"price\", DoubleRule) { price = it }\n      }\n    }\n  }\n}\n```\n\n## lib\n\n```\n                                     JSON.Instance\n                                           ^\n                                           |\n     +----------------+-------------+------+--------+----------------+----------------+\n     |                |             |               |                |                |\n JSON.Object      JSON.Array   JSON.String      JSON.Bool       JSON.Number       JSON.Null\n     ^                ^             ^               ^                ^                ^\n     |                |             |               |                |                |\n     |                |             |               |                |                |\nSimpleObject     SimpleArray   SimpleString     SimpleBool           |            SimpleNull\n                                                                     |\n                                                +--------------------+---------------------+\n                                                |                    |                     |\n                                           JSON.Integer          JSON.Long            JSON.Double \u003c---- SimpleDouble\n                                                ^                    ^                     ^\n                                                |                    |                     |\n                                                |                    |                     |\n                                           SimpleInteger         SimpleLong           JSON.Exp \u003c-------- SimpleExp\n\n\n                                          Parser\n                                             ^\n                                             |\n     +----------------+-------------+--------+------+----------------+--------------+\n     |                |             |               |                |              |\n     |                |             |               |                |              |\nObjectParser     ArrayParser   StringParser     BoolParser      NumberParser    NullParser\n\n\n                                         CharStream\n                                             ^\n                                             |\n                                +------------+--------------+\n                                |                           |\n                        CharArrayCharStream      UTF8ByteArrayCharStream\n\n\n                                     Rule ----------------------------\u003e DeserializeParserListener\n                                       ^\n                                       |\n                                       |\n     +----------------+-------------+---------------+----------------+-----------------------------+\n     |                |             |               |                |                             |\n     |                |             |               |                |                             |\n ObjectRule       ArrayRule     StringRule       BoolRule   +--------+--------+            NullableRule(Rule)\n                                                            |        |        |                    |\n                                                            |        |        |                    |\n                                                         IntRule  LongRule DoubleRule              |\n                                                                                                   |\n                                                      NullAsFalseBoolRule NullAsZeroIntRule NullAsZeroLongRule NullAsZeroDoubleRule NullableStringRule\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwkgcass%2Fvjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwkgcass%2Fvjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwkgcass%2Fvjson/lists"}