diff --git a/json-to-go.js b/json-to-go.js index 458a703..8fffdce 100644 --- a/json-to-go.js +++ b/json-to-go.js @@ -22,7 +22,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty try { - data = JSON.parse(json.replace(/:(\s*\d*)\.0/g, ":$1.1")); // hack that forces floats to stay as floats + data = JSON.parse(json.replace(/(:\s*\[?\s*-?\d*)\.0/g, "$1.1")); // hack that forces floats to stay as floats scope = data; } catch (e) diff --git a/json-to-go.test.js b/json-to-go.test.js index 105e19b..4005e43 100644 --- a/json-to-go.test.js +++ b/json-to-go.test.js @@ -78,6 +78,48 @@ function test(includeExampleData) { expectedWithExample: 'type AutoGenerated struct {\n\tAge int `json:"age" example:"46"`\n}' }, + { + input: '{"negativeFloat": -1.00}', + expected: + 'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat"`\n}', + expectedWithExample: + 'type AutoGenerated struct {\n\tNegativeFloat float64 `json:"negativeFloat" example:"-1.1"`\n}' + }, + { + input: '{"zeroFloat": 0.00}', + expected: + 'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat"`\n}', + expectedWithExample: + 'type AutoGenerated struct {\n\tZeroFloat float64 `json:"zeroFloat" example:"0.1"`\n}' + }, + { + input: '{"positiveFloat": 1.00}', + expected: + 'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat"`\n}', + expectedWithExample: + 'type AutoGenerated struct {\n\tPositiveFloat float64 `json:"positiveFloat" example:"1.1"`\n}' + }, + { + input: '{"negativeFloats": [-1.00, -2.00, -3.00]}', + expected: + 'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}', + expectedWithExample: + 'type AutoGenerated struct {\n\tNegativeFloats []float64 `json:"negativeFloats"`\n}' + }, + { + input: '{"zeroFloats": [0.00, 0.00, 0.00]}', + expected: + 'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}', + expectedWithExample: + 'type AutoGenerated struct {\n\tZeroFloats []float64 `json:"zeroFloats"`\n}' + }, + { + input: '{"positiveFloats": [1.00, 2.00, 3.00]}', + expected: + 'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}', + expectedWithExample: + 'type AutoGenerated struct {\n\tPositiveFloats []float64 `json:"positiveFloats"`\n}' + }, { input: '{"topLevel": { "secondLevel": "exampleDataHere"} }', expected: @@ -112,4 +154,4 @@ function test(includeExampleData) { } test(false); -test(true) +test(true) \ No newline at end of file