https://github.com/bgreni/EmberJson
A user friendly json library written in pure Mojo
https://github.com/bgreni/EmberJson
json json-parser
Last synced: about 2 months ago
JSON representation
A user friendly json library written in pure Mojo
- Host: GitHub
- URL: https://github.com/bgreni/EmberJson
- Owner: bgreni
- License: mit
- Created: 2024-10-10T16:04:58.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-03-05T04:47:55.000Z (2 months ago)
- Last Synced: 2025-03-16T08:31:39.348Z (about 2 months ago)
- Topics: json, json-parser
- Language: Mojo
- Homepage:
- Size: 1.61 MB
- Stars: 20
- Watchers: 1
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-mojo - EmberJson - An ergonomic JSON library in pure Mojo🔥. (🗂️ Libraries<a id='libraries'></a> / Web)
README
# EmberJson

A lightweight JSON parsing library for Mojo.
## Usage
### Parsing JSON
Use the `parse` function to parse a JSON value from a string. It accepts a
`ParseOptions` struct as a parameter to alter parsing behaviour.```mojo
from emberjson import parse
struct ParseOptions:
# ignore unicode for a small performance boost
var ignore_unicode: Bool...
var json = parse[ParseOptions(ignore_unicode=True)](r'["\uD83D\uDD25"]')
```EmberJSON supports decoding escaped unicode characters.
```mojo
print(parse(r'["\uD83D\uDD25"]')) # prints '["🔥"]'
```### Converting to String
Use the `to_string` function to convert a JSON struct to its string representation.
It accepts a parameter to control whether to pretty print the value.
The JSON struct also conforms to the `Stringable`, `Representable` and `Writable`
traits.```mojo
from emberjson import to_stringvar json = parse('{"key": 123}')
print(to_string(json)) # prints {"key":123}
print(to_string[pretty=True](json))
# prints:
#{
# "key": 123
#}
```### Working with JSON
`JSON` is the top level type for a document. It can contain either
an `Object` or `Array`.`Value` is used to wrap the various possible primitives that an object or
array can contain, which are `Int`, `Float64`, `String`, `Bool`, `Object`,
`Array`, and `Null`.```mojo
from emberjson import *var json = parse('{"key": 123}')
# check inner type
print(json.is_object()) # prints True# dict style access
print(json.object()["key"].int()) # prints 123# array
var array = parse('[123, 4.5, "string", True, null]').array()# array style access
print(array[3].bool()) # prints True# equality checks
print(array[4] == Null()) # prints True# None converts implicitly to Null
assert_equal(array[4], Value(None))# Implicit ctors for Value
var v: Value = "some string"# Convert Array and Dict back to stdlib types
# These are consuming actions so the original Array/Object will be moved
var arr = Array(123, False)
var l = arr.to_list()var ob = Object()
var d = ob.to_dict()
```