Skip to content

Commit

Permalink
Improve docs about converters
Browse files Browse the repository at this point in the history
- Fix compilation error in CustomIntConverter example.
- Add a note about `JsonReader.ValueSpan` usage.
  • Loading branch information
firenero committed Apr 13, 2024
1 parent d2b2ecd commit b380ba1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions website/docs/dev_guide/high_level/converters.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class CustomIntConverter : DdbConverter<int>
public override int Read(ref DdbReader reader)
{
if (!Utf8Parser.TryParse(reader.JsonReader.ValueSpan, out int value, out _))
throw new DdbException($"Couldn't parse int ddb value from '{reader.JsonReaderValue.GetString()}'.");
throw new DdbException($"Couldn't parse int ddb value from '{reader.JsonReader.GetString()}'.");

return value;
}
Expand All @@ -123,7 +123,7 @@ public class CustomIntConverter : DdbConverter<int>

public override int Read(in AttributeValue attributeValue) => attributeValue.AsNumberAttribute().ToInt();

public override AttributeValue Write(ref T value) => new NumberAttributeValue(value.ToString());
public override AttributeValue Write(ref int value) => new NumberAttributeValue(value.ToString());
}
```

Expand All @@ -133,6 +133,8 @@ public class CustomIntConverter : DdbConverter<int>

When a low-level read is called, `DdbReader.JsonReader` is already pointed to the JSON value. Current attribute type is automatically parsed and can be accessed using `DdbReader.AttributeType` property.

The `reader.JsonReader.HasValueSequence` is guaranteed to be false at this point, so it's safe to use `reader.JsonReader.ValueSpan` to access the JSON buffer.

The `DdbReader.JsonReader.Read` method should not be explicitly called unless you are writing a converter for a non-primitive JSON type like an object or array.

### JSON writing
Expand Down

0 comments on commit b380ba1

Please sign in to comment.