Skip to content

Commit e9bec3e

Browse files
dimodiTsvetomir-Hr
andauthored
kb(MediaQuery): Add KB about Json exception (#3014)
* kb(MediaQuery): Add KB about Json exception * Update knowledge-base/mediaquery-initmediaquery-jsonexception.md Co-authored-by: Tsvetomir Hristov <[email protected]> * Update knowledge-base/mediaquery-initmediaquery-jsonexception.md Co-authored-by: Tsvetomir Hristov <[email protected]> * Update knowledge-base/mediaquery-initmediaquery-jsonexception.md Co-authored-by: Tsvetomir Hristov <[email protected]> --------- Co-authored-by: Tsvetomir Hristov <[email protected]>
1 parent a392d63 commit e9bec3e

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

knowledge-base/chart-newtonsoft-seialization-settings.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,8 @@ public class ChartDataController : ControllerBase
199199
## Notes
200200

201201
The approach used internally by the Chart may change in the future. For example, at the time of writing, the new `System.Net.Http.Json` is not yet ready for use, but it may be used in the future. Thus, the approach described in this article may become unnecessary or wrong.
202+
203+
## See Also
204+
205+
* [DataSourceRequest Filters not Working When You Add Reporting or Newtonsoft.Json](slug:common-kb-newtonsoft-breaks-datasourcerequest-serialization)
206+
* [InitMediaQueryWidget Throws JsonException](slug:mediaquery-kb-initmediaquery-jsonexception)

knowledge-base/common-newtonsoft-breaks-datasourcerequest-serialization.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@ There are three other paths forward you can consider:
112112

113113
Telerik supports serialization of the `DataSourceRequest` as part of the [`Telerik.DataSource` package](slug:common-features-datasource-package) (which is used by UI for Blazor) with the `System.Text.Json` serializer only.
114114

115+
## See Also
116+
117+
* [Chart not Working with Newtonsoft.Json Properties](slug:chart-kb-newtonsoft-seialization-settings)
118+
* [InitMediaQueryWidget Throws JsonException](slug:mediaquery-kb-initmediaquery-jsonexception)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: InitMediaQueryWidget Throws JsonException
3+
description: Learn how to troubleshoot and find the cause for a JsonException that may be thrown by the Telerik InitMediaQueryWidget method.
4+
type: troubleshooting
5+
page_title: InitMediaQueryWidget Throws JsonException Due to Invalid Cast
6+
slug: mediaquery-kb-initmediaquery-jsonexception
7+
tags: blazor, mediaquery, serialization
8+
ticketid: 1676092, 1680874
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>
19+
MediaQuery for Blazor
20+
</td>
21+
</tr>
22+
</tbody>
23+
</table>
24+
25+
## Description
26+
27+
A Blazor app may throw a runtime JSON serialization exception on startup that is similar to:
28+
29+
```C#.skip-repl
30+
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: DeserializeUnableToConvertValue, System.Boolean Path: $ | LineNumber: 0 | BytePositionInLine: 4.. See InnerException for more details.
31+
32+
---> System.Text.Json.JsonException: DeserializeUnableToConvertValue, System.Boolean Path: $ | LineNumber: 0 | BytePositionInLine: 4.
33+
---> System.InvalidOperationException: InvalidCast, Null, boolean
34+
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedBoolean(JsonTokenType )
35+
at System.Text.Json.Utf8JsonReader.GetBoolean()
36+
37+
...
38+
39+
at Microsoft.JSInterop.JSRuntime.<InvokeAsync>.MoveNext()
40+
at Telerik.Blazor.Components.TelerikMediaQuery.InitMediaQueryWidget()
41+
at Telerik.Blazor.Components.TelerikMediaQuery.OnAfterRenderAsync(Boolean firstRender)
42+
```
43+
44+
or
45+
46+
```C#.skip-repl
47+
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: The JSON value could not be converted to System.Boolean. Path: $ | LineNumber: 0 | BytePositionInLine: 4.. See InnerException for more details.
48+
49+
---> System.Text.Json.JsonException: The JSON value could not be converted to System.Boolean. Path: $ | LineNumber: 0 | BytePositionInLine: 4.
50+
---> System.InvalidOperationException: Cannot get the value of a token type 'Null' as a boolean.
51+
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedBoolean(JsonTokenType tokenType)
52+
at System.Text.Json.Utf8JsonReader.GetBoolean()
53+
...
54+
55+
at Microsoft.JSInterop.JSRuntime.<InvokeAsync>.MoveNext()
56+
at Telerik.Blazor.Components.TelerikMediaQuery.InitMediaQueryWidget()
57+
at Telerik.Blazor.Components.TelerikMediaQuery.OnAfterRenderAsync(Boolean firstRender)
58+
```
59+
60+
## Cause
61+
62+
The [`TelerikRootComponent`](slug:rootcomponent-overview) creates a few [MediaQuery](slug:mediaquery-overview) components. These MediaQuery instances are responsible for the [adaptive behavior of all Telerik dropdowns and popups](slug:adaptive-rendering). During initialization, each Telerik MediaQuery component performs a JSInterop call in `OnAfterRenderAsync` that returns a boolean value back to the .NET runtime. This bool value shows whether the current browser viewport size matches the MediaQuery `Media` parameter value.
63+
64+
An JSON exception in the above algorithm indicates that the received value cannot be converted to boolean type. This can happen if the application is using a third-party package or middleware (for example, Serilog) that overrides the .NET serialization mechanism. As a result, the .NET runtime may receive `null` instead of `true` or `false`.
65+
66+
## Solution
67+
68+
The recommended approach is to modify the app configuration or third-party tooling, so that the .NET serialization works by default.
69+
70+
## Suggested Workaround
71+
72+
In some cases, it may be possible to avoid the JSON error by rendering all Razor components in the app with a delay in `OnAfterRenderAsync`:
73+
74+
>caption MainLayout.razor
75+
76+
````RAZOR.skip-repl
77+
@inherits LayoutComponentBase
78+
79+
@if (ShouldRenderApp)
80+
{
81+
<TelerikRootComponent>
82+
@Body
83+
</TelerikRootComponent>
84+
}
85+
86+
@code {
87+
private bool ShouldRenderApp { get; set; }
88+
89+
protected override async Task OnAfterRenderAsync(bool firstRender)
90+
{
91+
if (firstRender)
92+
{
93+
await Task.Delay(1);
94+
95+
ShouldRenderApp = true;
96+
StateHasChanged();
97+
}
98+
99+
await base.OnAfterRenderAsync(firstRender);
100+
}
101+
}
102+
````
103+
104+
## See Also
105+
106+
* [DataSourceRequest Filters not Working When You Add Reporting or Newtonsoft.Json](slug:common-kb-newtonsoft-breaks-datasourcerequest-serialization)
107+
* [Chart not Working with Newtonsoft.Json Properties](slug:chart-kb-newtonsoft-seialization-settings)
108+
* [Troubleshooting JavaScript Errors](slug:troubleshooting-js-errors)

0 commit comments

Comments
 (0)