@@ -425,7 +425,7 @@ local function query_(ast, pathlst, n)
425425 end
426426 end
427427 if n == # pathlst then
428- return data , k , isarray
428+ return ast , k , isarray
429429 end
430430 return query_ (data [k ], pathlst , n + 1 )
431431end
@@ -499,6 +499,19 @@ local function apply_array_insert_after(str, option, value, node)
499499 .. finish_text
500500end
501501
502+ local function apply_array_insert_empty (str , option , value , node )
503+ local start_text = str :sub (1 , node .s )
504+ local finish_text = str :sub (node .f - 1 )
505+ option .depth = option .depth + node .d + 1
506+ return start_text
507+ .. option .newline
508+ .. string_rep (option .indent , option .depth )
509+ .. json .beautify (value , option )
510+ .. option .newline
511+ .. string_rep (option .indent , option .depth - 1 )
512+ .. finish_text
513+ end
514+
502515local function apply_replace (str , option , value , node )
503516 local start_text = str :sub (1 , node .s - 1 )
504517 local finish_text = str :sub (node .f )
@@ -509,7 +522,7 @@ local function apply_replace(str, option, value, node)
509522end
510523
511524local function apply_object_insert (str , option , value , t , k )
512- local node = find_max_node (t )
525+ local node = find_max_node (t . v )
513526 if node then
514527 local start_text = str :sub (1 , node .f - 1 )
515528 local finish_text = str :sub (node .f )
@@ -523,6 +536,20 @@ local function apply_object_insert(str, option, value, t, k)
523536 .. ' ": '
524537 .. json .beautify (value , option )
525538 .. finish_text
539+ else
540+ local start_text = str :sub (1 , t .s )
541+ local finish_text = str :sub (t .f - 1 )
542+ option .depth = option .depth + t .d + 1
543+ return start_text
544+ .. option .newline
545+ .. string_rep (option .indent , option .depth )
546+ .. ' "'
547+ .. json ._encode_string (k )
548+ .. ' ": '
549+ .. json .beautify (value , option )
550+ .. option .newline
551+ .. string_rep (option .indent , option .depth - 1 )
552+ .. finish_text
526553 end
527554end
528555
@@ -553,14 +580,16 @@ function OP.add(str, option, path, value)
553580 return
554581 end
555582 if isarray then
556- if t [k ] then
557- return apply_array_insert_before (str , option , value , t [k ])
583+ if t .v [k ] then
584+ return apply_array_insert_before (str , option , value , t .v [k ])
585+ elseif k == 1 then
586+ return apply_array_insert_empty (str , option , value , t )
558587 else
559- return apply_array_insert_after (str , option , value , t [k - 1 ])
588+ return apply_array_insert_after (str , option , value , t . v [k - 1 ])
560589 end
561590 else
562- if t [k ] then
563- return apply_replace (str , option , value , t [k ])
591+ if t . v [k ] then
592+ return apply_replace (str , option , value , t . v [k ])
564593 else
565594 return apply_object_insert (str , option , value , t , k )
566595 end
@@ -577,15 +606,15 @@ function OP.remove(str, _, path)
577606 return
578607 end
579608 if isarray then
580- if k > # t then
609+ if k > # t . v then
581610 return
582611 end
583- return apply_remove (str , t [k ].s , t [k ].f )
612+ return apply_remove (str , t . v [k ].s , t . v [k ].f )
584613 else
585- if t [k ] == nil then
614+ if t . v [k ] == nil then
586615 return
587616 end
588- return apply_remove (str , t [k ].key_s , t [k ].f )
617+ return apply_remove (str , t . v [k ].key_s , t . v [k ].f )
589618 end
590619end
591620
@@ -601,11 +630,15 @@ function OP.replace(str, option, path, value)
601630 if not t then
602631 return
603632 end
604- if t [k ] then
605- return apply_replace (str , option , value , t [k ])
633+ if t . v [k ] then
634+ return apply_replace (str , option , value , t . v [k ])
606635 else
607636 if isarray then
608- return apply_array_insert_after (str , option , value , t [k - 1 ])
637+ if k == 1 then
638+ return apply_array_insert_empty (str , option , value , t )
639+ else
640+ return apply_array_insert_after (str , option , value , t .v [k - 1 ])
641+ end
609642 else
610643 return apply_object_insert (str , option , value , t , k )
611644 end
@@ -618,7 +651,7 @@ local function edit(str, patch, option)
618651 return
619652 end
620653 option = json .beautify_option (option )
621- return f (str , option , patch .path , patch .data )
654+ return f (str , option , patch .path , patch .value )
622655end
623656
624657json .edit = edit
0 commit comments