-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: auto convert non-string content value to JSON string under text mode #166
Conversation
Thanks Cloyd for providing this PR and looking into this. I'm not entirely sure if this is the right solution. Right now, you can provide two different content types: either |
(I suppose we can improve the error message to explain about |
Oh, I just tried using |
Yes indeed. The reason that there are two different content types for this is that if you receive a string, you cannot know for sure if it holds a stringified JSON document, or is a parsed JSON document holding just a string. The latter is a bit theoretical but I think it is good to have this explicitly modeled. I'll make the error message more informative and see if I can improve the docs. Do you have a suggestion on how to change the text "used when in text mode" to make it less confusing in this regard? Maybe just leave it out? |
I'm not sure about the 'hold' word you mentioned, do you mean we can't tell whether a string is a normal string like if (typeof content === 'string') {
if (content[0] === '{' && content.at(-1) === '}') {
const jsonContent = parseAndRepairOrUndefined(content)
if (jsonContent === undefined) {
// content is a normal string
} else {
// content is a JSON string
// we can treat it as JSON
}
} else {
// content is a normal string
}
} else {
// content is a JSON
} I believe a normal string starts with |
Yes indeed, it is an edge case, but a string by itself is also valid JSON. Trying to automatically detect whether a string was intended to be stringified JSON, or parsed JSON holding a string gives tricky edge cases. It will work in 95% of the cases I guess, but I prefer to explicitly model this to avoid any problems. It is similar to say the HTML Modeling the contents as Thoughts about your proposal:
It is tempting to hide this important difference (the contents being either stringified or parsed JSON) from the user, but I think the user needs to be aware of this. Other parts of the application will have assumptions about that too. If this difference is hidden from the user, it can be a tricky pitfall forgetting to parse/stringify in certain places, or accidentally stringifying JSON that is already stringified. What do you think? |
Thanks for the detailed reply. I found this in the document: 'In case of tree mode, json is used. In case of text mode, text is used', It seems
If so, do we eventually need to distinguish them, why don't we unify them and let users to do the I tried this example: |
Thanks, good feedback.
I see the documentation is confusion :) Let me try to improve the section in the docs that you're referring to:
Is that a bit understandable? 😅 (if not please help out)
That is correct, it is repaired and parsed since |
Yes, so we could only allow |
I've improved the docs via f7f3837, feedback welcome! |
I mean only |
So you mean like only allow passing a parsed JSON document ( That would make things nice and simple indeed, and this can work for the tree mode (which internally uses a parsed JSON document). However, when using I'm not sure if we're yet at the same page. Does that make sense to you why we need both |
😊 I understand |
👍
I tried to argue that also for the user of the library it is important to know whether dealing with a stringified or parsed document (instead of having a mix: sometimes being parsed and sometimes stringified). I myself would not unify the two content types. But if you think this is not a relevant thing for the users of |
OK we are clear about this. Here's what I considered about
|
From your conclusion I see that I did a bad job with my latest updates in the documentation. I see you took the relation between mode and content type literally, where as I had only intended that explanation to illustrate why there are two different content types in the first place. My bad, I should have added words like "mostly" there. It is not always the case that
Here a next iteration of the docs, I moved most of the explanation to
|
But |
Ok clear. @bestkolobok you may find it interesting too to read up on this topic. Not sure, but looking at vue3-jsoneditor it seems like the user too gets a mix of sometimes stringified and sometimes parsed JSON in the same variable. I think it's important to at least be aware of that. |
Hi @josdejong! Thanks for the detailed explanation of the text and json content in this thread. So I understand that the concept of binding the content of the view-editor needs some rethinking. At a minimum, I need to:
Now, after a busy working day, there are still no constructive thoughts. I will try to make the appropriate changes within the next two days and let you know about it. Thanks again for clarifying these important details. |
I should clarify too that the unification of <JsonEditorVue
:content="content" :onChange="updatedContent => {
content = updatedContent
}"
/> User can basically do whatever they can in |
I made the following changes to vue3-jsoneditor today:
Despite the presence of two separate v-model for each type of data, I still decided to leave the option to use the base v-model, since such an implementation can be useful for many users. |
@cloydlau that sounds good, I wasn't aware of @bestkolobok thanks for your updates, take it easy :). Your brief summary is correct:
|
Hello,
I received these issues cloydlau/json-editor-vue#11 & cloydlau/json-editor-vue#16 recently. Which all caused by using JSON value under text mode.
I'm wondering would it be a little bit weird if a JSON editor don't take JSON value and throw an Error?
My understanding is mode means writing mode and might have little to do with value type.
So I did a little change using the new
parser
option to stringify non-string content value under text mode rather than throwing an error. (lint
&test
passing)It can be quite common to use JSON value under text mode. In which case users will have to stringify the content value themselves and therefore the content value will be stringified anyway.