@@ -106,19 +106,24 @@ internal JContainer()
106
106
{
107
107
}
108
108
109
- internal JContainer ( JContainer other )
109
+ internal JContainer ( JContainer other , JsonCloneSettings ? settings )
110
110
: this ( )
111
111
{
112
112
ValidationUtils . ArgumentNotNull ( other , nameof ( other ) ) ;
113
113
114
+ bool copyAnnotations = settings ? . CopyAnnotations ?? true ;
115
+
116
+ if ( copyAnnotations )
117
+ {
118
+ CopyAnnotations ( this , other ) ;
119
+ }
120
+
114
121
int i = 0 ;
115
122
foreach ( JToken child in other )
116
123
{
117
- TryAddInternal ( i , child , false ) ;
124
+ TryAddInternal ( i , child , false , copyAnnotations ) ;
118
125
i ++ ;
119
126
}
120
-
121
- CopyAnnotations ( this , other ) ;
122
127
}
123
128
124
129
internal void CheckReentrancy ( )
@@ -323,7 +328,7 @@ internal bool IsMultiContent([NotNullWhen(true)]object? content)
323
328
return ( content is IEnumerable && ! ( content is string ) && ! ( content is JToken ) && ! ( content is byte [ ] ) ) ;
324
329
}
325
330
326
- internal JToken EnsureParentToken ( JToken ? item , bool skipParentCheck )
331
+ internal JToken EnsureParentToken ( JToken ? item , bool skipParentCheck , bool copyAnnotations )
327
332
{
328
333
if ( item == null )
329
334
{
@@ -341,15 +346,20 @@ internal JToken EnsureParentToken(JToken? item, bool skipParentCheck)
341
346
// the item is being added to the root parent of itself
342
347
if ( item . Parent != null || item == this || ( item . HasValues && Root == item ) )
343
348
{
344
- item = item . CloneToken ( ) ;
349
+ // Avoid allocating settings when copy annotations is false.
350
+ JsonCloneSettings ? settings = copyAnnotations
351
+ ? null
352
+ : JsonCloneSettings . SkipCopyAnnotations ;
353
+
354
+ item = item . CloneToken ( settings ) ;
345
355
}
346
356
347
357
return item ;
348
358
}
349
359
350
360
internal abstract int IndexOfItem ( JToken ? item ) ;
351
361
352
- internal virtual bool InsertItem ( int index , JToken ? item , bool skipParentCheck )
362
+ internal virtual bool InsertItem ( int index , JToken ? item , bool skipParentCheck , bool copyAnnotations )
353
363
{
354
364
IList < JToken > children = ChildrenTokens ;
355
365
@@ -360,7 +370,7 @@ internal virtual bool InsertItem(int index, JToken? item, bool skipParentCheck)
360
370
361
371
CheckReentrancy ( ) ;
362
372
363
- item = EnsureParentToken ( item , skipParentCheck ) ;
373
+ item = EnsureParentToken ( item , skipParentCheck , copyAnnotations ) ;
364
374
365
375
JToken ? previous = ( index == 0 ) ? null : children [ index - 1 ] ;
366
376
// haven't inserted new token yet so next token is still at the inserting index
@@ -490,7 +500,7 @@ internal virtual void SetItem(int index, JToken? item)
490
500
491
501
CheckReentrancy ( ) ;
492
502
493
- item = EnsureParentToken ( item , false ) ;
503
+ item = EnsureParentToken ( item , false , copyAnnotations : true ) ;
494
504
495
505
ValidateToken ( item , existing ) ;
496
506
@@ -635,17 +645,17 @@ internal virtual void ValidateToken(JToken o, JToken? existing)
635
645
/// <param name="content">The content to be added.</param>
636
646
public virtual void Add ( object ? content )
637
647
{
638
- TryAddInternal ( ChildrenTokens . Count , content , false ) ;
648
+ TryAddInternal ( ChildrenTokens . Count , content , false , copyAnnotations : true ) ;
639
649
}
640
650
641
651
internal bool TryAdd ( object ? content )
642
652
{
643
- return TryAddInternal ( ChildrenTokens . Count , content , false ) ;
653
+ return TryAddInternal ( ChildrenTokens . Count , content , false , copyAnnotations : true ) ;
644
654
}
645
655
646
656
internal void AddAndSkipParentCheck ( JToken token )
647
657
{
648
- TryAddInternal ( ChildrenTokens . Count , token , true ) ;
658
+ TryAddInternal ( ChildrenTokens . Count , token , true , copyAnnotations : true ) ;
649
659
}
650
660
651
661
/// <summary>
@@ -654,10 +664,10 @@ internal void AddAndSkipParentCheck(JToken token)
654
664
/// <param name="content">The content to be added.</param>
655
665
public void AddFirst ( object ? content )
656
666
{
657
- TryAddInternal ( 0 , content , false ) ;
667
+ TryAddInternal ( 0 , content , false , copyAnnotations : true ) ;
658
668
}
659
669
660
- internal bool TryAddInternal ( int index , object ? content , bool skipParentCheck )
670
+ internal bool TryAddInternal ( int index , object ? content , bool skipParentCheck , bool copyAnnotations )
661
671
{
662
672
if ( IsMultiContent ( content ) )
663
673
{
@@ -666,7 +676,7 @@ internal bool TryAddInternal(int index, object? content, bool skipParentCheck)
666
676
int multiIndex = index ;
667
677
foreach ( object c in enumerable )
668
678
{
669
- TryAddInternal ( multiIndex , c , skipParentCheck ) ;
679
+ TryAddInternal ( multiIndex , c , skipParentCheck , copyAnnotations ) ;
670
680
multiIndex ++ ;
671
681
}
672
682
@@ -676,7 +686,7 @@ internal bool TryAddInternal(int index, object? content, bool skipParentCheck)
676
686
{
677
687
JToken item = CreateFromContent ( content ) ;
678
688
679
- return InsertItem ( index , item , skipParentCheck ) ;
689
+ return InsertItem ( index , item , skipParentCheck , copyAnnotations ) ;
680
690
}
681
691
}
682
692
@@ -963,7 +973,7 @@ int IList<JToken>.IndexOf(JToken item)
963
973
964
974
void IList < JToken > . Insert ( int index , JToken item )
965
975
{
966
- InsertItem ( index , item , false ) ;
976
+ InsertItem ( index , item , false , copyAnnotations : true ) ;
967
977
}
968
978
969
979
void IList < JToken > . RemoveAt ( int index )
@@ -1046,7 +1056,7 @@ int IList.IndexOf(object? value)
1046
1056
1047
1057
void IList . Insert ( int index , object ? value )
1048
1058
{
1049
- InsertItem ( index , EnsureValue ( value ) , false ) ;
1059
+ InsertItem ( index , EnsureValue ( value ) , false , copyAnnotations : false ) ;
1050
1060
}
1051
1061
1052
1062
bool IList . IsFixedSize => false ;
0 commit comments