tidwall / tile38

Real-time Geospatial and Geofencing
https://tile38.com
MIT License
8.96k stars 561 forks source link

Invalid JSON created when setting a field to a string value like '000296'. #736

Closed Kilowhisky closed 1 month ago

Kilowhisky commented 2 months ago

Describe the bug When the OUTPUT is set to JSON and a field was set to a string value like '000296'. The tile38 serializer will automatically convert the field into a number and try and output 000296. This is invalid json though because you cannot have leading 0's in numbers according to the JSON spec. This creates errors with many of the client libraries that utilize JSON.parse() (i'm using tile38-ts).

To Reproduce

  1. Set output to JSON
  2. Set a field to a value of '000296'
  3. Get the record
  4. See that it converted the field to a numerical field with leading zeros.

Expected behavior Strip leading zeros when converting it to a number?

Logs

Tile38Error: Unexpected number in JSON at position 14454 image

Operating System (please complete the following information):

Additional context I can fix in insertion handler, but invalid JSON should not be created either way...

Kilowhisky commented 2 months ago

So this is tied to this issue: https://github.com/tidwall/tile38/issues/719 Turns out, that field is supposed to be "vehicle name". The vehicle just happens to be called '000296'...

So i'm not sure how to fix this besides moving the "vehicle name" property to under the "properties" object. But i'm not sure if i can do that as that object is currently inserted as a POINT and performance is critical.

Thoughts?

Kilowhisky commented 2 months ago

I'm just going to prepend the field value with vn:<name>. Its a hack, but it works.

Kilowhisky commented 2 months ago

Turns out _ is also a valid number character for Go but not for JSON....

Tile38Error: Unexpected token _ in JSON at position 11948

image

tidwall commented 1 month ago

I just pushed an update that will ensure that a numeric field value conforms to JSON.