Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Add json.toStringPretty (#55)
Browse files Browse the repository at this point in the history
* Add json.toStringPretty

* Add optional indentation

* Better changelog

* Update src/remodel_api/json.rs

Co-authored-by: Lucien Greathouse <[email protected]>
  • Loading branch information
Kampfkarren and LPGhatguy authored Jun 1, 2021
1 parent b0a6ad6 commit 5e644ed
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added support for CFrame ([#48](https://github.com/rojo-rbx/remodel/pull/48))
* Added support for Vector3, and improved Vector3int16 ([#46](https://github.com/rojo-rbx/remodel/pull/46))
* Added Color3.fromRGB(red, blue, green) ([#44](https://github.com/rojo-rbx/remodel/pull/44))
* Added `json.toStringPretty` ([#55](https://github.com/rojo-rbx/remodel/pull/55))

## 0.8.1 (2021-04-09)
* Updated to latest rbx_xml, which should fix `OptionalCoordinateFrame`-related issues.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ Encodes a Lua object as a JSON string. Can only encode Lua primitives like table

Throws on error, like if the input table cannot be encoded.

### `json.toStringPretty` (Unreleased)
```
json.toStringPretty(value: any, indent?: string = " "): string
```

Encodes a Lua object as a prettified JSON string. If an indent is passed, will use that for indentation, otherwise will default to two spaces.

Throws on error, like if the input table cannot be encoded.

## Supported Roblox Types
When interacting with Roblox instances, Remodel doesn't support all value types yet and may throw an error.

Expand Down
26 changes: 25 additions & 1 deletion src/remodel_api/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use rlua::{Context, FromLua, Table, ToLua, UserData, UserDataMethods, Value as LuaValue};
use serde_json::{Number, Value as JsonValue};
use serde::Serialize;
use serde_json::{
ser::{PrettyFormatter, Serializer},
Number, Value as JsonValue,
};

pub struct Json;

Expand All @@ -9,6 +13,26 @@ impl UserData for Json {
serde_json::to_string(&lua_value.0).map_err(rlua::Error::external)
});

methods.add_function(
"toStringPretty",
|_context, (lua_value, indent): (Value, Option<String>)| {
let pretty_formatter = if let Some(ref indent) = indent {
PrettyFormatter::with_indent(indent.as_bytes())
} else {
PrettyFormatter::new()
};

let mut output = Vec::new();
let mut serializer = Serializer::with_formatter(&mut output, pretty_formatter);
lua_value
.0
.serialize(&mut serializer)
.map_err(rlua::Error::external)?;

String::from_utf8(output).map_err(rlua::Error::external)
},
);

methods.add_function("fromString", |_context, source: String| {
serde_json::from_str::<JsonValue>(&source)
.map(Value)
Expand Down
32 changes: 31 additions & 1 deletion test-scripts/json-encode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,34 @@ assert(encodedSparse == "[1.0,2.0,null,4.0]")

local sparser = {1, 2, 3, [1000] = 6}
local encodedSparser = json.toString(sparser)
assert(encodedSparser == [[{"1":1.0,"1000":6.0,"2":2.0,"3":3.0}]])
assert(encodedSparser == [[{"1":1.0,"1000":6.0,"2":2.0,"3":3.0}]])

local encodedPretty = json.toStringPretty(baseline)
assert(encodedPretty == [[{
"w": {
"0": "cool",
"wack": "yo"
},
"x": 5.0,
"y": "Hello, world!",
"z": [
1.0,
2.0,
3.0
]
}]])

local encodedPrettyWithIndent = json.toStringPretty(baseline, "\t")
assert(encodedPrettyWithIndent == [[{
"w": {
"0": "cool",
"wack": "yo"
},
"x": 5.0,
"y": "Hello, world!",
"z": [
1.0,
2.0,
3.0
]
}]])

0 comments on commit 5e644ed

Please sign in to comment.