JSON.stringify(遞歸)與?JSON.parse(有限狀態(tài)自動機)的實現(xiàn)代碼

JSON 的 stringify 和 parse 兩個方法在平時的工作中也很常用,如果沒有一些特殊的類型,是實現(xiàn)數(shù)據(jù)深拷貝的一個原生方式。
下面就這兩個方法的一個手動實現(xiàn)思路。
JSON.stringify
JSON.stringify 方法用于將 JavaScript 值轉(zhuǎn)換為 JSON 字符串。該方法有三個參數(shù):
- data: 需要轉(zhuǎn)換的數(shù)據(jù)
- replacer:用于轉(zhuǎn)換結(jié)果的對象或者數(shù)組,可以函數(shù)或者數(shù)組
- space:文本添加縮進、空格和換行符,可以是數(shù)字或者字符串,數(shù)字的最大值是 10,字符串的最大長度是 10
下面的測試只用到這些類型: number,string,function,object,array,null,undefined,map,set,weakmap,weakset
但是 JavaScript 數(shù)據(jù)的嚴格類型遠遠不止這幾個。
data
首先我們用 JSON.stringify 來打印結(jié)果:
const testJson = {
4: 3,
n: 1,
s: 's',
f: () => { },
null: null,
unde: undefined,
arr: [1, 's', null, undefined, () => { }],
obj: {
n: '1',
s: 's'
},
map: new Map(),
set: new Set([1, 2, 3]),
wmap: new WeakMap(),
wset: new WeakSet()
}
const raws = JSON.stringify(testJson)
// {
// "4":3,"n":1,"s":"s","null":null,"arr":[1,"s",null,null,null],
// "obj":{"n":"1","s":"s"},"map":{},"set":{},"wmap":{},"wset":{}
// }根據(jù)上面的結(jié)果,我們可以發(fā)現(xiàn)對象內(nèi)的 function, undefined 被剔除了,map, set 等都被動的轉(zhuǎn)換成了空對象。而數(shù)組內(nèi)的 function 和 undefined 被替換成了 null。
所以我們可以根據(jù)上述規(guī)則寫一個簡單的 stringify 方法:
const stringify = (data: any) => {
// 獲取數(shù)據(jù)的嚴格類型
const type = getType(data)
let res = ''
switch (type) {
case 'Object':
// 處理對象
res = stringifyObject(data)
break
case 'Array':
// 處理數(shù)組
res = stringifyArray(data)
break
case 'Number':
case 'Boolean':
res = `${data}`
break
case 'String':
res = `"${data}"`
break
case 'Null':
res = 'null'
break
case 'Set':
case 'WeakSet':
case 'Map':
case 'WeakMap':
res = '{}'
break
default:
return
}
return res
}實現(xiàn)幾個輔助函數(shù):
// 獲取嚴格類型
const getType = (data: any) => {
return Object.prototype.toString.call(data).slice(8, -1)
}
// 處理對象方法
const stringifyObject = (data: Record<string, any>) => {
const vals: string[] = []
for (const key in data) {
// 遞歸處理
const val = stringify(data[key])
// 如果值為 undefined,我們則需要跳過
if (val !== undefined) {
vals.push(`"${key}":${val}`)
}
}
return `{${vals.join(',')}}`
}
// 處理數(shù)組方法
const stringifyArray = (data: any[]) => {
const vals: any[] = []
for (const val of data) {
// 遞歸處理,如果返回 undefined 則替換為 null
vals.push(stringify(val) || 'null')
}
return `[${vals.join(',')}]`
}到這里就實現(xiàn)了 stringify 的簡單版本。下面可以簡單測試一下:
const raws = JSON.stringify(testJson) const cuss = stringify(testJson) console.log(raws === cuss) // true
后面還有兩個參數(shù),我們先實現(xiàn)第三個,第二個參數(shù)的作用等下在實現(xiàn)。
space
space 主要是用于添加空格、換行、縮進,但是只要 space 的值是合法的,換行符是默認加上一個的。所以我們要改下 stringify 的方法:
type Replacer = ((key: string, value: any) => any) | null | (string | number)[]
export const stringify = (data: any, replacer?: Replacer, space?: number | string, indent = 1) => {
const type = getType(data)
if (typeof space === 'number') {
if (space <= 0) {
space = undefined
} else {
space = Math.min(10, space)
}
} else if (typeof space === 'string') {
space = space.substring(0, 10)
} else if (space) {
space = undefined
}
let res = ''
switch (type) {
case 'Object':
res = stringifyObject(data, indent, replacer, space)
break
case 'Array':
res = stringifyArray(data, indent, replacer, space)
break
// 省略部分代碼
}
// 省略部分代碼
}對于 space 的不同非法的值,我們可以在控制臺上進行一些簡單的測試就可以得出,像 -1 這種其實是不生效的。而我處理的是只能是數(shù)字和字符串,數(shù)字必須是 1 - 10,字符串的最長長度是 10 位,其余的都重置為 undefined。因為像數(shù)組和對象的這種嵌套,縮進其實是要跟著動的,這里就新增了 indent 字段,初始為 1,后續(xù)遞歸就 + 1。
// 新增分隔符處理方法
const handleSeparator = (space: number | string, indent: number, prefix: string = '', suffix: string = '') => {
let separator = prefix + '\n'
if (typeof space === 'number') {
separator += ' '.repeat(space).repeat(indent)
} else {
separator += space.repeat(indent)
}
return separator + suffix
}
// 對象方法修改
const stringifyObject = (data: Record<string, any>, indent: number, replacer?: Replacer, space?: number | string) => {
const vals: string[] = []
for (const key in data) {
const val = stringify(data[key], replacer, space, indent + 1)
if (val !== undefined) {
vals.push(`"${key}":${space ? ' ' : ''}${val}`)
}
}
// 新增 space 處理
if (space) {
const val = vals.join(handleSeparator(space, indent, ','))
if (!val) {
return '{}'
}
const front = handleSeparator(space, indent, '{')
const back = handleSeparator(space, indent - 1, '', '}')
return front + val + back
}
return `{${vals.join(',')}}`
}
// 數(shù)組處理方法
const stringifyArray = (data: any[], indent: number, replacer?: Replacer, space?: number | string) => {
const vals: any[] = []
for (const val of data) {
vals.push(stringify(val, replacer, space, indent + 1) || 'null')
}
// 新增 space 處理
if (space) {
const front = handleSeparator(space, indent, '[')
const val = vals.join(handleSeparator(space, indent, ','))
const back = handleSeparator(space, indent - 1, '', ']')
return front + val + back
}
return `[${vals.join(',')}]`
}replacer
replacer 參數(shù)有兩個類型:
- 數(shù)組類型是用來過濾對象類型內(nèi)的字段,只保留數(shù)組內(nèi)的 key
- 函數(shù)類型就是在數(shù)組和對象遍歷的時候,開發(fā)者可以自定義某些類型的字符串方式
所以這里我們需要修改三處地方:
// 起始方法增加一個是否是第一次調(diào)用的標記 init
export const stringify = (data: any, replacer?: Replacer, space?: number | string, indent = 1, init = true) => {
// 如果 replacer 為函數(shù)的話,初始 key 為空串,值為 data
if (typeof replacer === 'function' && init) {
return stringify(replacer('', data), replacer, space, indent, false)
}
const type = getType(data)
// 省略部分代碼
}
const stringifyObject = (data: Record<string, any>, indent: number, replacer?: Replacer, space?: number | string) => {
const filter = getType(replacer) === 'Array' ? replacer : null
const vals: string[] = []
for (const key in data) {
// 補全參數(shù),修改 replacer 處理
const val = stringify(
typeof replacer === 'function' ? replacer(key, data[key]) : data[key],
replacer,
space,
indent + 1,
false
)
if (
val !== undefined &&
(
!filter ||
(filter as (string | number)[]).includes(key) ||
(filter as (string | number)[]).includes(+key)
)
) {
vals.push(`"${key}":${space ? ' ' : ''}${val}`)
}
}
// 省略部分代碼
}
const stringifyArray = (data: any[], indent: number, replacer?: Replacer, space?: number | string) => {
const vals: any[] = []
let i = 0
for (const val of data) {
// 補全參數(shù),修改 replacer 處理
vals.push(stringify(
typeof replacer === 'function' ? replacer(i++, val) : val,
replacer,
space,
indent + 1,
false
) || 'null')
}
if (space) {
const front = handleSeparator(space, indent, '[')
const val = vals.join(handleSeparator(space, indent, ','))
const back = handleSeparator(space, indent - 1, '', ']')
return front + val + back
}
return `[${vals.join(',')}]`
}到這里, stringify 的方法差不多了。下面是完整代碼:
type Replacer = ((key: string | number, value: any) => any) | null | (string | number)[]
const getType = (data: any) => {
return Object.prototype.toString.call(data).slice(8, -1)
}
const handleSeparator = (space: number | string, indent: number, prefix: string = '', suffix: string = '') => {
let separator = prefix + '\n'
if (typeof space === 'number') {
separator += ' '.repeat(space).repeat(indent)
} else {
separator += space.repeat(indent)
}
return separator + suffix
}
const stringifyObject = (data: Record<string, any>, indent: number, replacer?: Replacer, space?: number | string) => {
const filter = getType(replacer) === 'Array' ? replacer : null
const vals: string[] = []
for (const key in data) {
const val = stringify(
typeof replacer === 'function' ? replacer(key, data[key]) : data[key],
replacer,
space,
indent + 1,
false
)
if (
val !== undefined &&
(
!filter ||
(filter as (string | number)[]).includes(key) ||
(filter as (string | number)[]).includes(+key)
)
) {
vals.push(`"${key}":${space ? ' ' : ''}${val}`)
}
}
if (space) {
const val = vals.join(handleSeparator(space, indent, ','))
if (!val) {
return '{}'
}
const front = handleSeparator(space, indent, '{')
const back = handleSeparator(space, indent - 1, '', '}')
return front + val + back
}
return `{${vals.join(',')}}`
}
const stringifyArray = (data: any[], indent: number, replacer?: Replacer, space?: number | string) => {
const vals: any[] = []
let i = 0
for (const val of data) {
vals.push(stringify(
typeof replacer === 'function' ? replacer(i++, val) : val,
replacer,
space,
indent + 1,
false
) || 'null')
}
if (space) {
const front = handleSeparator(space, indent, '[')
const val = vals.join(handleSeparator(space, indent, ','))
const back = handleSeparator(space, indent - 1, '', ']')
return front + val + back
}
return `[${vals.join(',')}]`
}
export const stringify = (data: any, replacer?: Replacer, space?: number | string, indent = 1, init = true): string | undefined => {
if (typeof replacer === 'function' && init) {
return stringify(replacer('', data), replacer, space, indent, false)
}
const type = getType(data)
if (typeof space === 'number') {
if (space <= 0) {
space = undefined
} else {
space = Math.min(10, space)
}
} else if (typeof space === 'string') {
space = space.substring(0, 10)
} else if (space) {
space = undefined
}
let res = ''
switch (type) {
case 'Object':
res = stringifyObject(data, indent, replacer, space)
break
case 'Array':
res = stringifyArray(data, indent, replacer, space)
break
case 'Number':
res = `${data}`
break
case 'Boolean':
res = `${data}`
break
case 'String':
res = `"${data}"`
break
case 'Null':
res = 'null'
break
case 'Set':
case 'WeakSet':
case 'Map':
case 'WeakMap':
res = '{}'
break
default:
return
}
return res
}JSON.parse
stringify 方法的實現(xiàn)還是比較簡單的,在一些筆試中還有可能會有相關(guān)需要實現(xiàn)的題。
而 JSON.parse 則是需要將合法的 json 字符串轉(zhuǎn)換成對象,這里就需要用到一個概念:有限狀態(tài)自動機
有限狀態(tài)自動機
這里只做簡單的介紹:有限狀態(tài)機(Finite State Machine),是指任意時刻都處于有限狀態(tài)集合中的某一狀態(tài)。當其獲得一個輸入字符時,將從當前狀態(tài)轉(zhuǎn)換到另一個狀態(tài)或者仍然保持當前狀態(tài)。
可以結(jié)合當前 json 字符串的場景來簡單理解一下:
我們有如下一個字符串:
const str = '{"4":3,"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'然后定義幾個狀態(tài):
const State = {
INIT: 'INIT', // 初始狀態(tài)
OBJECTSTART: 'OBJECTSTART', // 開始解析對象
ARRAYSTART: 'ARRAYSTART', // 開始解析數(shù)組
OBJVALSTART: 'OBJVALSTART', // 開始解析對象的屬性與值
OBJVALEND: 'OBJVALEND', // 對象屬性與值解析結(jié)束
ARRVALSTART: 'ARRVALSTART' // 開始解析數(shù)組值
}因為 json 字符串是非常規(guī)則的字符串,所以我們可以結(jié)合正則表達式來提取相關(guān)步驟的數(shù)據(jù),在字符串中的 ' '\t\n\r 等也是可以的,所以在正則中需要考慮并且替換。
const parse = (data: string | number | null || boolean) => {
if (typeof data === 'number' || data === null || typeof data === 'boolean') {
return data
}
// 將字符串轉(zhuǎn)換為地址引用,方便后面字符串數(shù)據(jù)的消費
const context = { data }
// 具體解析方法
return parseData(context)
}然后定義幾個輔助函數(shù):
// 字符串的消費函數(shù) - 就是截取已匹配完的數(shù)據(jù),返回剩余字符串
const advance = (context: { data: string }, num: number) => {
context.data = context.data.slice(num)
}
// 是否結(jié)束狀態(tài)機
const isEnd = (ctx: { data: string }) => {
// 如果沒有數(shù)據(jù)了,則結(jié)束
if (!ctx.data) {
return false
}
const match = /^([}\]])[ \t\n\r]*/.exec(ctx.data)
if (match) {
if (
match[1] === '}' && getType(res) !== 'Object' ||
match[1] === ']' && getType(res) !== 'Array'
) {
throw Error('解析錯誤')
}
advance(ctx, match[0].length)
return false
}
return true
}
// 處理值
const parseValue = (context: { data: string }, match: any[]) => {
advance(context, match[0].length)
const valMatch = /^"(.*?)"$/.exec(match[1])
if (valMatch) {
return valMatch[1]
}
if (match[1] === 'null') {
return null
}
if (match[1] === 'true') {
return true
}
if (match[1] === 'false') {
return false
}
if (isNaN(+match[1])) {
throw Error('解析錯誤')
}
return Number(match[1])
}
// 解析對象屬性值
const parseObjValue = (context: { data: string }) => {
const match = /^[ \n\t\r]*((".*?")|([0-9A-Za-z]*))[ \t\n\r]*/.exec(context.data)
if (match) {
return parseValue(context, match)
}
new Error('解析錯誤')
}
// 解析數(shù)組值
const parseArrValue = (context: { data: string }) => {
const refMatch = /^({|\[[ \n\t\r]*)/.exec(context.data)
if (refMatch) {
return parseData(context)
}
const match = /^((".*?")|([0-9a-zA-Z]*))[ \n\t\r]*[,]?[ \n\t\r]*/.exec(context.data)
if (match) {
return parseValue(context, match)
}
throw Error('解析錯誤')
}在上面定義狀態(tài)的時候,解析對象、數(shù)組和數(shù)組值的時候只有開始狀態(tài),而沒有結(jié)束狀態(tài)。只是結(jié)束狀態(tài)統(tǒng)一放入 isEnd 函數(shù)中,。
解析流程
下面開始定義 parseData 函數(shù):
第一步
const parseData = (ctx: { data: string }) => {
let res: any = ''
let currentState = State.INIT
while (isEnd(ctx, res)) {
switch (currentState) {
case State.INIT:
{
const match = /^[ \t\n\r]*/.exec(ctx.data)
if (match?.[0].length) {
advance(ctx, match[0].length)
}
if (ctx.data[0] === '{') {
res = {}
currentState = State.OBJECTSTART
} else if (ctx.data[0] === '[') {
res = []
currentState = State.ARRAYSTART
} else {
res = parseObjValue(ctx)
}
}
break
case State.OBJECTSTART:
break
case State.OBJVALSTART:
break
case State.OBJVALEND:
break
case State.ARRAYSTART:
break
case State.ARRVALSTART:
break
// no default
}
}
return res
}INIT 中,先去掉前面的空格、換行等字符,示例:
// 消費前
const str1 = ' \t\n\r{"4":3,"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'
// 消費后
const str2 = '{"4":3,"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'然后再判讀第一個字符是什么:
- 如果是
{,則將狀態(tài)轉(zhuǎn)移到OBJECTSTART,將res賦值一個空對象 - 如果是
[,則將狀態(tài)轉(zhuǎn)移到ARRAYSTART,將res賦值一個空數(shù)組 - 如果都不是,則就是一個值,可以用對象解析屬性值的方法來解析,判讀是否是合法的字符串
所以這里的狀態(tài)轉(zhuǎn)移到了對象解析 OBJECTSTART:
第二步
const parseData = (ctx: { data: string }) => {
let res: any = ''
let currentState = State.INIT
while (isEnd(ctx)) {
switch (currentState) {
case State.INIT:
// 省略部分代碼
break
case State.OBJECTSTART:
{
const match = /^{[ \t\n\r]*/.exec(ctx.data)
if (match) {
advance(ctx, match[0].length)
currentState = State.OBJVALSTART
}
}
break
case State.OBJVALSTART:
break
case State.OBJVALEND:
break
case State.ARRAYSTART:
break
case State.ARRVALSTART:
break
// no default
}
}
return res
}OBJECTSTART 中,消費掉 {,將狀態(tài)轉(zhuǎn)移到 OBJVALSTART, 剩余字符數(shù)據(jù):
const str = '"4":3,"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'第三步
const parseData = (ctx: { data: string }) => {
let res: any = ''
let currentState = State.INIT
while (isEnd(ctx)) {
switch (currentState) {
case State.INIT:
// 省略部分代碼
break
case State.OBJECTSTART:
// 省略部分代碼
break
case State.OBJVALSTART:
{
const match = /^"(.*?)"[ \n\t\r]*:[ \n\t\r]*/.exec(ctx.data)
if (match) {
advance(ctx, match[0].length)
if (ctx.data[0] === '{' || ctx.data[0] === '[') {
res[match[1]] = parseData(ctx)
} else {
res[match[1]] = parseObjValue(ctx)
}
currentState = State.OBJVALEND
}
}
break
case State.OBJVALEND:
break
case State.ARRAYSTART:
break
case State.ARRVALSTART:
break
// no default
}
}
return res
}先獲取 key: 等數(shù)組并消費,剩余字符數(shù)據(jù):
const str = '3,"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'先判讀后續(xù)字符的第一個字符是什么:
- 如果是
{或者[,則開啟一個新的狀態(tài)機 - 否則直接用
parseObjValue解析值
最后將狀態(tài)轉(zhuǎn)移至 OBJVALEND。
第四步
const parseData = (ctx: { data: string }) => {
let res: any = ''
let currentState = State.INIT
while (isEnd(ctx)) {
switch (currentState) {
case State.INIT:
// 省略部分代碼
break
case State.OBJECTSTART:
// 省略部分代碼
break
case State.OBJVALSTART:
// 省略部分代碼
break
case State.OBJVALEND:
{
const match = /^[ \t\n\r]*(,)[ \t\n\r]*/.exec(ctx.data)
if (match) {
if (match[1] === ',') {
currentState = State.OBJVALSTART
}
advance(ctx, match[0].length)
}
}
break
case State.ARRAYSTART:
break
case State.ARRVALSTART:
break
// no default
}
}
return res
}如果后面匹配出來的字符是 ,,則表示后續(xù)還有其它的對象屬性,我們需要將狀態(tài)重新轉(zhuǎn)移到 OBJVALSTART, 如果是其它的 } 或者 ],則會在此次消費完畢,然后在 isEnd 中會退出狀態(tài)機。
后續(xù)剩余字符的變化會依照上數(shù)狀態(tài)的變化而進行字符消費:
const str = '3,"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'
// 1
const str = ',"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'
// 2
const str = '"s":"s","null":null,"arr":[1,"s",null],"obj":{}}'
// 省略 s 和 null
// 3 開啟新的狀態(tài)機
const str = '[1,"s",null],"obj":{}}'
// 4 結(jié)束狀態(tài)機
const str = '],"obj":{}}'
// 5 開啟新的狀態(tài)機
const str = '{}}'
// 6 結(jié)束狀態(tài)機
const str = '}}'
// 7 結(jié)束狀態(tài)機
const str = '}'數(shù)組的處理
const parseData = (ctx: { data: string }) => {
let res: any = ''
let currentState = State.INIT
while (isEnd(ctx)) {
switch (currentState) {
case State.INIT:
// 省略部分代碼
break
case State.OBJECTSTART:
// 省略部分代碼
break
case State.OBJVALSTART:
// 省略部分代碼
break
case State.OBJVALEND:
// 省略部分代碼
break
case State.ARRAYSTART:
{
const match = /^\[[ \t\n\r]*/.exec(ctx.data)
if (match) {
advance(ctx, match[0].length)
currentState = State.ARRVALSTART
}
}
break
case State.ARRVALSTART:
res.push(parseArrValue(ctx))
break
// no default
}
}
return res
}如果第一個字符為 [,則會開啟新的狀態(tài)機,狀態(tài)也會轉(zhuǎn)換為 ARRAYSTART,然后在 ARRAYSTART 狀態(tài)內(nèi)進行數(shù)組值的轉(zhuǎn)換。
到這里整個 JSON.parse 的實現(xiàn)思路差不多,但是上述的流程應(yīng)該有沒考慮到的地方,但是大體差不多,只是邊界的處理問題。測試示例:
// 數(shù)據(jù)使用上面的 testJson const raws = JSON.stringify(testJson) const rawp = JSON.parse(raws) const cusp = parse(raws) console.log(raws, 'JSON.stringify') console.log(rawp, 'JSON.parse') console.log(cusp, 'parse')
結(jié)果:

完整代碼
const State = {
INIT: 'INIT',
OBJECTSTART: 'OBJECTSTART',
ARRAYSTART: 'ARRAYSTART',
OBJVALSTART: 'OBJVALSTART',
OBJVALEND: 'OBJVALEND',
ARRVALSTART: 'ARRVALSTART'
}
const isEnd = (ctx: { data: string }, res: any) => {
if (!ctx.data) {
return false
}
const match = /^([}\]])[ \t\n\r]*/.exec(ctx.data)
if (match) {
if (
match[1] === '}' && getType(res) !== 'Object' ||
match[1] === ']' && getType(res) !== 'Array'
) {
throw Error('解析錯誤')
}
advance(ctx, match[0].length)
return false
}
return true
}
const advance = (context: { data: string }, num: number) => {
context.data = context.data.slice(num)
}
const parseValue = (context: { data: string }, match: any[]) => {
advance(context, match[0].length)
const valMatch = /^"(.*?)"$/.exec(match[1])
if (valMatch) {
return valMatch[1]
}
if (match[1] === 'null') {
return null
}
if (match[1] === 'true') {
return true
}
if (match[1] === 'false') {
return false
}
if (isNaN(+match[1])) {
throw Error('解析錯誤')
}
return Number(match[1])
}
const parseObjValue = (context: { data: string }) => {
const match = /^[ \n\t\r]*((".*?")|([0-9A-Za-z]*))[ \t\n\r]*/.exec(context.data)
if (match) {
return parseValue(context, match)
}
new Error('解析錯誤')
}
const parseArrValue = (context: { data: string }) => {
const refMatch = /^({|\[[ \n\t\r]*)/.exec(context.data)
if (refMatch) {
return parseData(context)
}
const match = /^((".*?")|([0-9a-zA-Z]*))[ \n\t\r]*[,]?[ \n\t\r]*/.exec(context.data)
if (match) {
return parseValue(context, match)
}
throw Error('解析錯誤')
}
const parseData = (ctx: { data: string }) => {
let res: any = ''
let currentState = State.INIT
while (isEnd(ctx, res)) {
switch (currentState) {
case State.INIT:
{
const match = /^[ \t\n\r]*/.exec(ctx.data)
if (match?.[0].length) {
advance(ctx, match[0].length)
}
if (ctx.data[0] === '{') {
res = {}
currentState = State.OBJECTSTART
} else if (ctx.data[0] === '[') {
res = []
currentState = State.ARRAYSTART
} else {
res = parseObjValue(ctx)
}
}
break
case State.OBJECTSTART:
{
const match = /^{[ \t\n\r]*/.exec(ctx.data)
if (match) {
advance(ctx, match[0].length)
currentState = State.OBJVALSTART
}
}
break
case State.OBJVALSTART:
{
const match = /^"(.*?)"[ \n\t\r]*:[ \n\t\r]*/.exec(ctx.data)
if (match) {
advance(ctx, match[0].length)
if (ctx.data[0] === '{' || ctx.data[0] === '[') {
res[match[1]] = parseData(ctx)
} else {
res[match[1]] = parseObjValue(ctx)
}
currentState = State.OBJVALEND
}
}
break
case State.OBJVALEND:
{
const match = /^[ \t\n\r]*(,)[ \t\n\r]*/.exec(ctx.data)
if (match) {
if (match[1] === ',') {
currentState = State.OBJVALSTART
}
advance(ctx, match[0].length)
}
}
break
case State.ARRAYSTART:
{
const match = /^\[[ \t\n\r]*/.exec(ctx.data)
if (match) {
advance(ctx, match[0].length)
currentState = State.ARRVALSTART
}
}
break
case State.ARRVALSTART:
res.push(parseArrValue(ctx))
break
// no default
}
}
return res
}
export const parse = (data: string | number | null | boolean) => {
if (typeof data === 'number' || data === null || typeof data === 'boolean') {
return data
}
const context = { data }
return parseData(context)
}到此這篇關(guān)于JSON.stringify(遞歸)與 JSON.parse(有限狀態(tài)自動機)的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)JSON.stringify與 JSON.parse實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實現(xiàn)的CRC32函數(shù)示例
這篇文章主要介紹了JavaScript實現(xiàn)的CRC32函數(shù),簡單介紹了CRC32函數(shù)的概念和功能,并給出實例形式分析了CRC32函數(shù)的javascript實現(xiàn)與使用方法,需要的朋友可以參考下2016-11-11
javascript 漢字轉(zhuǎn)拼音實現(xiàn)代碼
主要是提前定義了很多文字,這樣的方法不是很是,如果庫中沒有這個詞,是看不到相關(guān)信息的。2009-12-12
用javascript實現(xiàn)的仿Flash廣告圖片輪換效果
用javascript實現(xiàn)的仿Flash廣告圖片輪換效果...2007-04-04
JavaScript中toLocaleString()和toString()的區(qū)別實例分析
這篇文章主要介紹了JavaScript中toLocaleString()和toString()的區(qū)別,結(jié)合實例形式對比分析了toLocaleString()和toString()針對字符串、數(shù)組與日期操作過程中的區(qū)別與使用技巧,需要的朋友可以參考下2018-08-08

