Skip to content

update to expose contexts#645

Closed
gregsdennis wants to merge 4 commits into
native-aotfrom
native-aot-better-use-of-context
Closed

update to expose contexts#645
gregsdennis wants to merge 4 commits into
native-aotfrom
native-aot-better-use-of-context

Conversation

@gregsdennis

@gregsdennis gregsdennis commented Jan 31, 2024

Copy link
Copy Markdown
Collaborator

Really just want to show the Logic lib. The rest isn't quite ready.

This showcases that we can expose the contexts, which gives the user access to the JTIs for all of the types contained in or used by the lib. We still use the options manager to handle deserializing user-registered rules, but it's kept internal. The hard part is remembering on which context your JTI exists.

Also, because the context is exposed, there's no need to have the IJsonTypeInfoResolver static property.

Ultimately, I see this working out for all the libs. We can also create a serializer context in Json.More that exposes common types, like JsonNode. (Really, I think .Net should expose a context for its common types... *cough* @eiriktsarpalis)

We might also be able to drop the IJsonConverterReadWrite.

@github-actions

github-actions Bot commented Jan 31, 2024

Copy link
Copy Markdown

Test Results

    15 files  ±0      15 suites  ±0   12s ⏱️ -2s
20 478 tests ±0  19 114 ✅ ±0  1 364 💤 ±0  0 ❌ ±0 
20 570 runs  ±0  19 203 ✅ ±0  1 367 💤 ±0  0 ❌ ±0 

Results for commit 075c705. ± Comparison against base commit fef56e4.

♻️ This comment has been updated with latest results.

Comment thread JsonSchema.OpenApi/DiscriminatorKeyword.cs Outdated
Comment thread JsonLogic/Rule.cs Outdated
@gregsdennis gregsdennis changed the title update logic to expose context update to expose contexts Feb 1, 2024
/// <param name="options">An object that specifies serialization options to use.</param>
void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options);
/// <param name="typeInfo">The <see cref="JsonTypeInfo"/> for the value being serialized.</param>
void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options, JsonTypeInfo? typeInfo = null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not require it for Read too?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read is working. I didn't need to update it. I can look at doing that, though.

@jevansaks

Copy link
Copy Markdown
Contributor
  where T : Rule

Now that we are targeting latest System.Text.Json, we can change this parameter on AddRule<T> and RegisterKeyword<T> to be an IJsonTypeInfoResolver. I had only made it a JsonSerializerContext because STJ6 didn't have the interface.


Refers to: JsonLogic/RuleRegistry.cs:118 in 075c705. [](commit_id = 075c705, deletion_comment = False)


_keywords = new ConcurrentDictionary<string, Type>(keywordData.ToDictionary(x => x.Item2, x => x.Item1));
_keywordTypeInfoResolvers = new ConcurrentDictionary<Type, JsonSerializerContext>(keywordData.ToDictionary(x => x.Item1, _ => (JsonSerializerContext)JsonSchemaSerializerContext.Default));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuleRegistry did this differently and just returns null for known types. It seems more efficient to not populate this dictionary and just return the default context when the keyword type isn't found.

return _keywordTypeInfoResolvers.TryGetValue(ruleType, out var context)
? context.GetTypeInfo(ruleType)!
: JsonSchemaSerializerContext.Default.GetTypeInfo(typeof(UnrecognizedKeyword))!;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this ignoring ruleType here and passing UnrecognizedKeyword explicitly?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the keyword isn't found, it's treated as an unrecognized keyword. This captures the data for the keyword. It doesn't play into validation and just generates and annotation.

);
static JsonSchemaDataSerializerContext()
{
CombinedOptions = new JsonSerializerOptions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to combine options anymore? This new strategy should avoid the need for it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the workaround for the bug. I need it in the UriIdentifier class where I'm serializing a JsonSchema.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which bug? Can you include a comment / pointer to the issue if it's a bug in STJ?

{
PropertyNameCaseInsensitive = true
});
var collections = JsonSerializer.Deserialize<List<TestCollection>>(contents, _serializerOptions);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, this call should be able to just pass DataTestsSerializerContext.Default.ListTestCollection, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to be able to set the options here.

@gregsdennis

Copy link
Copy Markdown
Collaborator Author

closing in favor of #648 which includes these changes.

@gregsdennis gregsdennis closed this Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants