Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
smittytone committed Jun 6, 2019
2 parents f8aeb1a + 6c456e0 commit f85b441
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 64 deletions.
15 changes: 10 additions & 5 deletions JSONParser.class.nut
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @author Mikhail Yurasov <[email protected]>
* @package JSONParser
* @version 0.3.1
* @version 1.0.1
*/

/**
Expand All @@ -13,7 +13,7 @@
class JSONParser {

// should be the same for all components within JSONParser package
static version = [1, 0, 0];
static version = "1.0.1";

/**
* Parse JSON string into data structure
Expand Down Expand Up @@ -164,10 +164,15 @@ class JSONParser {

":" : {
colon = function () {
// check if the key already exists
if (key in container) {
throw "Duplicate key \"" + key + "\"";
// Check if the key already exists
// NOTE previous code used 'if (key in container)...'
// but this finds table ('container') member methods too
local err = false;
foreach (akey, avalue in container) {
if (akey == key) err = true;
break
}
if (err) throw "Duplicate key \"" + key + "\"";
state = "ovalue";
}
},
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt → LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Electric Imp, Inc.
Copyright (c) 2016-19 Electric Imp, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
119 changes: 61 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,75 @@
# Squirrel JSON Parser
# Squirrel JSON Parser 1.0.1 #

This library parses JSON into Squirrel data types.

**To add this library to your project, add** `#require "JSONParser.class.nut:1.0.0"` **to the top of your code.**
**To include this library in your project, add** `#require "JSONParser.class.nut:1.0.1"` **at the top of your code.**

![Build Status](https://cse-ci.electricimp.com/app/rest/builds/buildType:(id:JSONParser_BuildAndTest)/statusIcon)

## Usage
## Usage ##

JSONParser has no constructor and one public function, *parse()*.

### parse(*jsonString[, converterFunction]*)
### parse(*jsonString[, converter]*)

The *parse()* method takes one required parameter, a JSON encoded string, and one optional parameter: a function used to convert custom types. The method returns a deserialized version of the object that was passed in.
This method converts the supplied JSON to a table.

#### Basic Example
#### Parameters ####

```squirrel
local jsonString = "{\"one\" : 1}";
result <- JSONParser.parse(jsonString);
server.log(result.one);
// Displays '1'
```
| Parameter | Type | Required? | Description |
| --- | --- | --- | --- |
| *jsonString* | String | Yes | The JSON to parse |
| *converter* | Function | No | A function used to convert custom types. See [**Custom Types Converter**](#custom-type-converter), below, for more information |

### Custom Types Converter
#### Custom Types Converter ####

The optional converter function can be used to deserialize custom types. It takes two parameters:
An optional converter function can be passed into *parse()* to de-serialize custom types. The function has two parameters:

- *value* &mdash; String representation of a value
- *type* &mdash; String indicating conversion type: `"string"` or `"number"`
- *value* &mdash; String representation of a value.
- *type* &mdash; String indicating conversion type: `"string"` or `"number"`.

For example, the following code converts all numbers to floats and makes strings uppercase:

```squirrel
result <- JSONParser.parse(jsonString, function (value, type) {
if (type == "number") {
return val.tofloat();
} else if (type == "string") {
return val.toupper();
}
if (type == "number") {
return val.tofloat();
} else if (type == "string") {
return val.toupper();
}
});
```

#### Extended Example:
#### Return Value ####

Table &mdash; the full de-serialized data.

#### Basic Example ####

```squirrel
local jsonString = "{\"one\" : 1}";
result <- JSONParser.parse(jsonString);
server.log(result.one);
// Displays '1'
```

#### Extended Example ####

```squirrel
class MyCustomType {
_value = null;
_value = null;
constructor(value) {
this._value = value;
}
constructor(value) {
this._value = value;
}
function _serialize() {
return "@mycustomtype:" + this._value;
}
function _serialize() {
return "@mycustomtype:" + this._value;
}
function getValue() {
return this._value;
}
function getValue() {
return this._value;
}
}
o <- {a = 1, b = "Something", c = MyCustomType("100500") };
Expand All @@ -68,16 +79,16 @@ server.log(s);
// Displays '{"a":1,"c":"@mycustomtype:100500","b":"Something"}'
result <- JSONParser.parse(s, function (val, type) {
if ("number" == type) {
return val.tofloat();
} else if ("string" == type) {
if (null != val.find("@mycustomtype")) {
// Convert my custom type
val = MyCustomType(val.slice(14))
if ("number" == type) {
return val.tofloat();
} else if ("string" == type) {
if (null != val.find("@mycustomtype")) {
// Convert my custom type
val = MyCustomType(val.slice(14))
}
return val;
}
return val;
}
});
server.log(result.c instanceof MyCustomType);
Expand All @@ -87,26 +98,18 @@ server.log(result.c.getValue());
// Displays '100500'
```

## Testing

Repository contains [impUnit](https://github.com/electricimp/impUnit) tests and a configuration for [impTest](https://github.com/electricimp/impTest) tool.

Tests can be launched with:

```bash
imptest test
```
## Testing ##

By default configuration for the testing is read from [.imptest](https://github.com/electricimp/impTest/blob/develop/docs/imptest-spec.md).
This repository contains automated tests that can be run on the command line using [*impt*](https://github.com/electricimp/imp-central-impt). For documentation on how to configure and run tests please see the [impt testing guide](https://github.com/electricimp/imp-central-impt/blob/master/TestingGuide.md).

To run test with your settings (for example while you are developing), create your copy of **.imptest** file and name it something like **.imptest.local**, then run tests with:
Test configuration is stored in the `.impt.test` file. To run tests locally:

```bash
imptest test -c .imptest.local
```
- Update the *deviceGroupId* to a Device Group ID from your impCentral account.
- Use *impt* commands to log into your impCentral account.
- Run tests using the `impt test run` command.

Tests do not require any specific hardware.
Tests do not require any specific hardware or environment variables. Please do not include modified `.impt.test` configuration files when submitting pull requests to this repository.

## License
## License ##

The code in this repository is licensed under [MIT License](https://github.com/electricimp/serializer/tree/master/LICENSE). Partially based on Douglas Crockford's [state-machine JSON parser](https://github.com/douglascrockford/JSON-js/blob/master/json_parse_state.js) available as public domain.
This library is licensed under the [MIT License](LICENSE). It is Partially based on Douglas Crockford's [state-machine JSON parser](https://github.com/douglascrockford/JSON-js/blob/master/json_parse_state.js) available as public domain.

0 comments on commit f85b441

Please sign in to comment.