josdejong / jsoneditor

A web-based tool to view, edit, format, and validate JSON
http://jsoneditoronline.org
Apache License 2.0
11.53k stars 2.04k forks source link

Explicit strings in editor #862

Open bturner1273 opened 4 years ago

bturner1273 commented 4 years ago

There should be an option for explicit strings in the editor. For example say I have a string representing an integer number i.e. '12345', the only way I would be able to represent this in the editor currently without messing up my validation (checks for string) would be '\'123\'' which is kind of annoying.

If there was an option for explicit strings a user would need to have quotes around the string value they are setting and if they wanted a ' or " in their string they would need to put the escape sequence \' or \" respectively. If this is already an option please let me know.

josdejong commented 4 years ago

This is an existing option: you can give a field the type String instead of the default Auto. Is that what you mean?

bturner1273 commented 4 years ago

Yeah you're right thank you, setting the field type to string lets the user enter numbers correctly so that isn't an issue. However when you have a type string for a field in your schema but the value of your json (following the aforementioned schema) is an empty string ("" or '') and you try to set the json editor contents to it you get empty 'value' boxes instead of "" or ''. Is that the behavior you were looking for?

bturner1273 commented 4 years ago

The preview when you go to filter/transform the data is correct though it is just those value boxes in the view that I think could confuse someone because a valid string (empty) is there but it looks like there is nothing

bturner1273 commented 4 years ago

Going into code mode fixes this issue but I really don't want a user to be editing json without a nice UI like tree mode. I am just hoping tree mode could handle empty strings slightly differently

bturner1273 commented 4 years ago

This basically boils down to an option that would be:

(options = {explicit_strings: false;}) input/display your strings like this in tree mode: asdf <-current format

(options = {explicit_strings: true;}) input/display strings like this in tree mode: "asdf" or 'asdf' (whatever yah want) <- explicit string format

this would allow empty strings to display as they do in code mode

josdejong commented 4 years ago

This is an interesting topic, I think there is definitely room for improvement here!

First: in the basis, JSONEditor is created as a free-form JSON editor, similar to a code editor where you can enter whatever text you like. To help check whether your JSON is valid, JSON schema validation and custom validation options are added. Still, it's all free form and just meant to support the user, not restrict the user.

Without a schema, the editor does not have information to determine whether a value like 123 should be a number or a string. The current behavior is to turn a value into a number when it is parsable as a number. We can improve this behavior if we use context information. You mention two ideas:

  1. When a value was originally a string (empty or not), it should stay a string and not be turned into a number.
  2. When we have a JSON schema, we can possibly determine the expected type (number or string) from the schema.

Important: as soon as the "Auto" behavior sometimes chooses to make a field a string, we should have a way for the user to override this behavior. So like we can currently set a field type to "String", we have to be able to set a field type explicitly to "Number".

RysinStas commented 4 years ago

This is an existing option: you can give a field the type String instead of the default Auto. Is that what you mean?

Hello @josdejong How can i do this?

josdejong commented 4 years ago

@RysinStas click on the context menu left from a node, open the "Type", then selet "String" instead of "Auto".

afbeelding

JonnyLeiZhang commented 2 years ago

Hi @josdejong I'd like to know is it supported now?

When a value was originally a string (empty or not), it should stay a string and not be turned into a number.
When we have a JSON schema, we can possibly determine the expected type (number or string) from the schema.

BR/Lei

josdejong commented 2 years ago

@JonnyLeiZhang there is (still) no support for utilizing JSON Schema to determine what type a field should have.

Help would be welcome