@@ -229,10 +229,31 @@ procedure TSimbaJSONItemHelper.SetAsUnicodeString(AValue: UnicodeString);
229229end ;
230230
231231procedure TSimbaJSONItemHelper.SetKey (Index: Integer; AValue: String);
232+ var
233+ Items: array of record
234+ Key: TJSONStringType;
235+ Value : TJSONData;
236+ end ;
237+ I: Integer;
232238begin
233239 CheckIsObject();
240+ if (Index < 0 ) or (Index >= TJSONObject(Self).Count) then
241+ SimbaException(' Index %d is out of range' , [Index]);
242+
243+ // not ideal since FPC doesn't provide access to FHashList but should be fine
244+ // no new objects are made, just rehashing
245+ SetLength(Items, TJSONObject(Self).Count);
246+ I := High(Items); // looping down probs more efficent for deletes
247+ while (TJSONObject(Self).Count > 0 ) do
248+ begin
249+ Items[I].Key := TJSONObject(Self).Names[I];
250+ Items[I].Value := TJSONObject(Self).Extract(I);
251+ Dec(I);
252+ end ;
234253
235- TJSONObject(Self).Add(AValue, TJSONObject(Self).Extract(Index));
254+ Items[Index].Key := AValue;
255+ for I := 0 to High(Items) do
256+ TJSONObject(Self).Add(Items[I].Key, Items[I].Value );
236257end ;
237258
238259procedure TSimbaJSONItemHelper.Add (AKey: String; Value : TSimbaJSONItem);
@@ -461,7 +482,7 @@ procedure TSimbaJSONItemHelper.Save(FileName: String; Options: EJSONFormatOption
461482 else
462483 Stream := TFileStream.Create(FileName, fmCreate);
463484 try
464- Stream.Write(Text[1 ], Length(Text));
485+ Stream.Size := Stream. Write(Text[1 ], Length(Text));
465486 finally
466487 Stream.Free();
467488 end ;
0 commit comments