翼度科技»论坛 编程开发 JavaScript 查看内容

react-json-editor-ajrm解析错误与解决方案

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
背景

由于历史原因,项目中 JSON 编辑器使用的是 react-json-editor-ajrm,近期遇到一个严重的展示错误,传入编辑器的数据与展示的不一致,这是产品和用户不可接受的。


工具介绍

react-json-editor-ajrm 可以用于查看、编辑和校验 JSON 对象。但这个项目已经不再积极维护,并计划在2023年6月15日废弃。
  1. <a href="https://github.com/AndrewRedican/react-json-editor-ajrm" rel="external nofollow"  rel="external nofollow"   target="_blank">https://github.com/AndrewRedican/react-json-editor-ajrm</a>
复制代码
  1. <strong>Warning:</strong> As you may already know, the react-json-editor-ajrm's orignal project is not actively maintained and that it will eventually <strong>be deprecated</strong>. So I've decided to set an official date for deprecation. The tentative date for this is June 15, 2023.
复制代码
问题复现

使用官方示例
  1. <a href="https://github.com/AndrewRedican/react-json-editor-ajrm/blob/master/example/create-react-app-project/src/index.js" rel="external nofollow"   target="_blank">https://github.com/AndrewRedican/react-json-editor-ajrm/blob/master/example/create-react-app-project/src/index.js</a>
复制代码
这里仅把测试数据换成能复现问题的数据(在解析嵌套带引号数据时会出问题)
  1. export const testData = {
  2.   "key1": "{"test":"{"name":"editor"}"}",
  3.   "key2": "{"name":"editor"}",
  4.   "key3": {
  5.     "name": "editor"
  6.   }
  7. }
复制代码
  1. import React, { Component } from "react";
  2. import ReactDOM from "react-dom";
  3. import "./index.css";

  4. import JSONInput from "react-json-editor-ajrm/index";
  5. import locale from "react-json-editor-ajrm/locale/en";

  6. import { testData } from "./testData";

  7. class App extends Component {
  8.   render() {
  9.     /**
  10.      * Rendering this JSONInput component with some properties
  11.      */
  12.     return (
  13.       <div style={{ maxWidth: "1400px", maxHeight: "100%" }}>
  14.         <JSONInput
  15.           placeholder={testData} // data to display
  16.           theme="light_mitsuketa_tribute"
  17.           locale={locale}
  18.           colors={{
  19.             string: "#DAA520" // overrides theme colors with whatever color value you want
  20.           }}
  21.           height="550px"
  22.           onChange={(e) => {
  23.             console.log("jsoneditor-onchange-e", e);
  24.           }}
  25.         />
  26.       </div>
  27.     );
  28.   }
  29. }

  30. ReactDOM.render(<App />, document.querySelector("#root"));
复制代码
渲染效果如图:


很明显能看出问题,key1、key2 的展示都跟原始数据不一致

探究原因

这是用一个常用的 JSON 格式化工具的展示效果。证明数据是没问题的,而是 react-json-editor-ajrm 内部处理逻辑导致的问题。

深入分析 react-json-editor-ajrm 源码,发现 this.tokenize 函数在处理传入数据时出现了问题。这导致了数据标记(tokens)的生成错误,进一步导致 markupText 的错误,最终影响了数据的展示。

分析链路


  • render 函数中,dangerouslySetInnerHTML: this.createMarkup(markupText)


  • showPlaceholder 函数中
  1. const data = this.tokenize(placeholder);
  2.     this.setState({
  3.       prevPlaceholder: placeholder,
  4.       plainText: data.indentation,
  5.       markupText: data.markup,
  6.       lines: data.lines,
  7.       error: data.error
  8.     });
复制代码

  • placeholder 是传入的数据
  • markupText 取自 this.tokenize(placeholder),然后更新


  • 关键在于 this.tokenize 对 placeholder 的处理,这里直接给出 this.tokenize 调用后的结果,感兴趣的可以查看源码

markup
  1. "<span type="symbol" value="{" depth="1" style="color:#D4D4D4">{</span>
  2.   <span type="key" value="key1" depth="1" style="color:#59A5D8">key1</span><span type="symbol" value=":" depth="1" style="color:#49B8F7">:</span> <span type="string" value="'{'" depth="1" style="color:#DAA520">'{'</span><span type="" value="" depth="1" style="color:#D4D4D4"></span><span type="string" value="':'" depth="1" style="color:#DAA520">':'</span><span type="symbol" value="{" depth="2" style="color:#D4D4D4">{</span>
  3.     <span type="key" value="'name\'" depth="2" style="color:#59A5D8">'name\'</span><span type="symbol" value=":" depth="2" style="color:#49B8F7">:</span> <span type="string" value="'editor\'" depth="2" style="color:#DAA520">'editor\'</span>
  4.   <span type="symbol" value="}" depth="1" style="color:#D4D4D4">}</span><span type="key" value="'}'" depth="1" style="color:#59A5D8">'}'</span><span type="symbol" value="," depth="1" style="color:#D4D4D4">,</span>
  5.   <span type="key" value="key2" depth="1" style="color:#59A5D8">key2</span><span type="symbol" value=":" depth="1" style="color:#49B8F7">:</span> <span type="string" value="'{'" depth="1" style="color:#DAA520">'{'</span><span type="" value="" depth="1" style="color:#D4D4D4"></span><span type="string" value="':'" depth="1" style="color:#DAA520">':'</span><span type="" value="" depth="1" style="color:#D4D4D4"></span><span type="string" value="'}'" depth="1" style="color:#DAA520">'}'</span><span type="symbol" value="," depth="1" style="color:#D4D4D4">,</span>
  6.   <span type="key" value="key3" depth="1" style="color:#59A5D8">key3</span><span type="symbol" value=":" depth="1" style="color:#49B8F7">:</span> <span type="symbol" value="{" depth="2" style="color:#D4D4D4">{</span>
  7.     <span type="key" value="name" depth="2" style="color:#59A5D8">name</span><span type="symbol" value=":" depth="2" style="color:#49B8F7">:</span> <span type="string" value="'editor'" depth="2" style="color:#DAA520">'editor'</span>
  8.   <span type="symbol" value="}" depth="1" style="color:#D4D4D4">}</span>
  9. <span type="symbol" value="}" depth="0" style="color:#D4D4D4">}</span>"
复制代码
解决方案

由于这是 react-json-editor-ajrm 内部处理逻辑导致的,所以只能考虑更换依赖包。
调研发现可以使用 jsoneditor-react,这里给出简单的示例:
  1. import { JsonEditor as Editor } from 'jsoneditor-react';
  2. import 'jsoneditor-react/es/editor.min.css';

  3. import React from 'react'
  4. import { testData } from './testData';
  5. function App() {
  6.   return (
  7.     <Editor
  8.         value={testData}
  9.         onChange={(val) => {
  10.           console.log("jsoneditor-react-val", val);
  11.         }}
  12.     />
  13.   )
  14. }

  15. export default App
复制代码

项目启动后,发现展示是符合预期的,也没有别的问题,可以使用 jsoneditor-react 作为替换的三方包。

工具对比

react-json-editor-ajrm vs jsoneditor-react
  1. <a href="https://npmtrends.com/jsoneditor-react-vs-react-json-editor-ajrm" rel="external nofollow"   target="_blank">https://npmtrends.com/jsoneditor-react-vs-react-json-editor-ajrm</a>
复制代码
npmtrends.com/ 中对两个工具的下载趋势进行了对比
pkg简介star地址react-json-editor-ajrmA stylish, editor-like, modular, react component for viewing, editing, and debugging javascript object syntax!354<a href="https://github.com/AndrewRedican/react-json-editor-ajrm" rel="external nofollow"  rel="external nofollow"   target="_blank">https://github.com/AndrewRedican/react-json-editor-ajrm</a>jsoneditor-reactreact wrapper implementation for jsoneditor262https://github.com/vankop/jsoneditor-reactjsoneditor-11.3khttps://github.com/josdejong/jsoneditor虽然从下载量以及 GitHub star 数量来看,jsoneditor-react 并不如 react-json-editor-ajrm,但 jsoneditor-react 是基于 jsoneditor 二次封装的,所以稳定性还是有一定的保障。
以上就是react-json-editor-ajrm解析错误与解决方案的详细内容,更多关于react-json-editor-ajrm错误的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/javascript/32350649b.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具