{"id":18151973,"url":"https://github.com/furnace-dev/sonic-mojo","last_synced_at":"2025-06-12T19:04:39.068Z","repository":{"id":260258245,"uuid":"880683897","full_name":"furnace-dev/sonic-mojo","owner":"furnace-dev","description":"A high-performance JSON processing library for the Mojo programming language, providing bindings for the Sonic-RS library and leveraging SIMD for fast JSON parsing and manipulation.","archived":false,"fork":false,"pushed_at":"2025-02-19T14:23:34.000Z","size":988,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T14:22:04.395Z","etag":null,"topics":["json","mojo","rust","sonic-rs"],"latest_commit_sha":null,"homepage":"","language":"Mojo","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/furnace-dev.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-30T06:51:39.000Z","updated_at":"2025-03-10T10:28:48.000Z","dependencies_parsed_at":"2025-02-19T15:27:06.702Z","dependency_job_id":"59e9795f-ee2a-44f0-adf0-c8c1ccc0154f","html_url":"https://github.com/furnace-dev/sonic-mojo","commit_stats":null,"previous_names":["f0cii/sonic-mojo","furnace-dev/sonic-mojo"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/furnace-dev%2Fsonic-mojo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/furnace-dev%2Fsonic-mojo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/furnace-dev%2Fsonic-mojo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/furnace-dev%2Fsonic-mojo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/furnace-dev","download_url":"https://codeload.github.com/furnace-dev/sonic-mojo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251667094,"owners_count":21624431,"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","mojo","rust","sonic-rs"],"created_at":"2024-11-02T02:05:11.817Z","updated_at":"2025-06-12T19:04:39.058Z","avatar_url":"https://github.com/furnace-dev.png","language":"Mojo","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sonic-mojo\r\nA high-performance JSON processing library for the Mojo programming language, providing bindings for the Sonic-RS library and leveraging SIMD for fast JSON parsing and manipulation.\r\n\r\n## Usage\r\n\r\n### add \r\n\r\n- channel: \r\n  - https://prefix.dev/channels/furnace-dev\r\n  - https://prefix.dev/channels/furnace-dev-nightly\r\n- add channel:\r\n\r\n```bash\r\n# for libsonic \u0026 sonic\r\nmagic project channel add \"https://repo.prefix.dev/furnace-dev\" \r\n\r\n# for sonic-mojo + nightly\r\nmagic project channel add \"https://repo.prefix.dev/furnace-dev-nightly\"\r\n\r\n\r\n```\r\n\r\n- add package:\r\n\r\n```bash\r\n# add rust binary lib (required for sonic-mojo)\r\nmagic add libsonic\r\n\r\n# add mojopkg\r\nmagic add sonic\r\n\r\n\r\n```\r\n\r\n### Release \r\n\r\n```ruby\r\n# install requirements\r\nmagic i\r\n\r\n\r\n# build rust libsonic \r\ntask br\r\n  \r\n# build mojo lib \r\ntask bm\r\n\r\n# publish rust libsonic\r\ntask publish:ffi\r\n\r\n\r\n# publish mojo lib\r\ntask publish:mojo \r\n\r\n  \r\n   \r\n```\r\n\r\n\r\n\r\n\r\n\r\n### Compile the sonic-rs library\r\n\r\n```bash\r\ncargo build --release\r\n```\r\n\r\n### Generate bindings for sonic-rs\r\n\r\n```bash\r\ncargo run --release -- generator\r\n```\r\n\r\n### Update the files in the internal directory to use the correct library name\r\n\r\n```bash\r\nsed -i 's/libXXX/libsonic/g' ./sonic/internal/diplomat_runtime.mojo\r\n```\r\n\r\n### Run the tests\r\n\r\n```bash\r\ncp ./target/release/libsonic.so ./\r\n\r\nmagic shell\r\nexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(realpath .)\r\nmojo test test_sonic.mojo\r\n```\r\n\r\n### Package the sonic-mojo library\r\n\r\n```bash\r\nmojo package ./sonic -o ./sonic_mojo.mojopkg\r\n```\r\n\r\n## Examples\r\n\r\nHere’s a quick example of how to use Sonic-Mojo to read and write JSON data:\r\n\r\n### Reading JSON\r\n\r\n```mojo\r\nfn main() raises:\r\n    var ctx = get_sonic_context()\r\n    # Create a JSON object\r\n    var o = JsonObject(\r\n        ctx,\r\n        (\r\n            '{\"i64\": 1000, \"u64\": 1000000000000000000, \"b\": true, \"s\": \"Hi\",'\r\n            ' \"obj\": {\"a\": 100, \"s\": \"hello\"}, \"arr\": [1,2,3], \"s_arr\": [\"a\",'\r\n            ' \"b\", \"c\"], \"null\": null}'\r\n        ),\r\n    )\r\n\r\n    # Reading\r\n    var i64 = o.get_i64(\"i64\")\r\n    var u64 = o.get_u64(\"u64\")\r\n    var b = o.get_bool(\"b\")\r\n    var s = o.get_str(\"s\")\r\n\r\n    # Reading nested object\r\n    var obj = o.get_object_mut(\"obj\")\r\n    var obj_a = obj.get_i64(\"a\")\r\n    var obj_s = obj.get_str(\"s\")\r\n\r\n    # Reading array\r\n    var arr = o.get_array_mut(\"arr\")\r\n    var arr_0 = arr.get_i64(0)\r\n    var arr_1 = arr.get_i64(1)\r\n    var arr_2 = arr.get_i64(2)\r\n\r\n    # Reading string array\r\n    var s_arr = o.get_array_mut(\"s_arr\")\r\n    var s_arr_len = s_arr.len()\r\n    var s_arr_0 = s_arr.get_str(0)\r\n    var s_arr_1 = s_arr.get_str(1)\r\n    var s_arr_2 = s_arr.get_str(2)\r\n\r\n    # Handling null value\r\n    var null = o.get_value(\"null\")\r\n    var i64_2 = o.get_value(\"i64\")\r\n\r\n    # Printing results\r\n    print(\"i64: \", i64)\r\n    print(\"u64: \", u64)\r\n    print(\"bool: \", b)\r\n    print(\"string: \", s)\r\n    print(\"obj.a: \", obj_a)\r\n    print(\"obj.s: \", obj_s)\r\n    print(\"arr[0]: \", arr_0)\r\n    print(\"arr[1]: \", arr_1)\r\n    print(\"arr[2]: \", arr_2)\r\n    print(\"s_arr length: \", s_arr_len)\r\n    print(\"s_arr[0]: \", s_arr_0)\r\n    print(\"s_arr[1]: \", s_arr_1)\r\n    print(\"s_arr[2]: \", s_arr_2)\r\n    print(\"null is null: \", null.is_null())\r\n    print(\"i64 is null: \", i64_2.is_null())\r\n```\r\n\r\n### Writing JSON\r\n\r\n```mojo\r\nfn main() raises:\r\n    var ctx = get_sonic_context()\r\n    # Create a JSON object\r\n    var o = JsonObject(ctx, '{\"a\": {\"b\": {\"c\": 100}}}')\r\n\r\n    # Get the nested object\r\n    var a = o.get_object_mut(\"a\")\r\n    var b = a.get_object_mut(\"b\")\r\n\r\n    # Read the current value\r\n    var c = b.get_i64(\"c\")\r\n\r\n    # Output the current value\r\n    print(\"Initial value of c: \", c)\r\n\r\n    # Write a new value\r\n    o.insert_bool(\"f\", True)\r\n    o.insert_u64(\"g\", 1000000000000000000)\r\n    o.insert_i64(\"d\", 101)\r\n    o.insert_str(\"e\", \"hello\")\r\n    o.insert_array(\"h\", JsonArray())\r\n    o.insert_object(\"i\", JsonObject('{\"j\": 100}'))\r\n\r\n    # Get the updated object\r\n    var a_2 = o.get_object_mut(\"a\")\r\n\r\n    # Print the string representation of the nested object\r\n    print(\"Updated object a: \", a_2.to_string())\r\n\r\n    # Print the string representation of the entire JSON object\r\n    print(\"Updated JSON object: \", o.to_string())\r\n```\r\n\r\n## Contributing\r\n\r\nContributions are welcome! If you have suggestions for improvements or new features, please open an issue or submit a pull request. Make sure to follow the contribution guidelines.\r\n\r\n## License\r\n\r\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.\r\n\r\n## Acknowledgments\r\n\r\n- [Sonic-RS](https://github.com/cloudwego/sonic-rs) for providing the foundation of this library.\r\n- [Diplomat](https://github.com/rust-diplomat/diplomat) for the code generation tool used to create Mojo bindings, with modifications in my forked version [f0cii/diplomat](https://github.com/f0cii/diplomat).\r\n- The community for their support and contributions.\r\n\r\n---\r\n\r\nFeel free to modify any sections to better fit your project or add any additional information you think is necessary!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffurnace-dev%2Fsonic-mojo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffurnace-dev%2Fsonic-mojo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffurnace-dev%2Fsonic-mojo/lists"}