-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
foo = 'hello' | ||
test = '{ "name": "Kai", "age": 34, "is_admin": true, "hobbies": ["programming", "gaming", "reading"], "address": { "street": "1234 Main St", "city": "Anytown", "state": "CA", "zip": 12345 } }' | ||
|
||
bar = { | ||
foo: 'bar' | ||
} | ||
foo = json.parse(test) | ||
|
||
print(bar.foo) | ||
print(foo) | ||
print(bar) | ||
console.log(foo.name) | ||
console.log(foo.age) | ||
console.log(foo.is_admin) | ||
console.log(foo.hobbies) | ||
console.log(foo.address) | ||
console.log(foo.address.street) | ||
console.log(foo.address.city) | ||
console.log(foo.address.state) | ||
console.log(foo.address.zip) | ||
console.log(foo.hobbies[0]) | ||
console.log(foo.hobbies[1]) | ||
console.log(foo.hobbies[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package modules | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"ghostlang.org/x/ghost/object" | ||
"ghostlang.org/x/ghost/token" | ||
) | ||
|
||
var JsonMethods = map[string]*object.LibraryFunction{} | ||
var JsonProperties = map[string]*object.LibraryProperty{} | ||
|
||
func init() { | ||
RegisterMethod(JsonMethods, "decode", jsonDecode) | ||
// RegisterMethod(JsonMethods, "encode", jsonEncode) | ||
} | ||
|
||
// jsonDecode decodes the JSON-encoded data and returns a new list or map object. | ||
func jsonDecode(scope *object.Scope, tok token.Token, args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return object.NewError("wrong number of arguments. got=%d, want=1", len(args)) | ||
} | ||
|
||
str, ok := args[0].(*object.String) | ||
|
||
if !ok { | ||
return object.NewError("argument to `decode` must be STRING, got %s", args[0].Type()) | ||
} | ||
|
||
var data interface{} | ||
|
||
err := json.Unmarshal([]byte(str.Value), &data) | ||
|
||
if err != nil { | ||
return object.NewError("failed to decode JSON: %s", err.Error()) | ||
} | ||
|
||
switch v := data.(type) { | ||
case []interface{}: | ||
var elements []object.Object | ||
|
||
for _, val := range v { | ||
elements = append(elements, object.AnyValueToObject(val)) | ||
} | ||
|
||
return &object.List{Elements: elements} | ||
case map[string]interface{}: | ||
pairs := make(map[object.MapKey]object.MapPair) | ||
|
||
for key, val := range v { | ||
pairKey := &object.String{Value: key} | ||
pairValue := object.AnyValueToObject(val) | ||
|
||
pairs[pairKey.MapKey()] = object.MapPair{Key: pairKey, Value: pairValue} | ||
} | ||
|
||
return &object.Map{Pairs: pairs} | ||
} | ||
|
||
return object.NewError("failed to decode JSON: %s", err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters