Skip to content

Commit e3dfe20

Browse files
committed
Merge pull request #43 from BartAdv/json-settings
Add possibility to set own JsonSerializerSettings in configuration
2 parents b710d1a + 32d8470 commit e3dfe20

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

src/React.Tests/Core/ReactComponentTest.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void RenderHtmlShouldThrowExceptionIfComponentDoesNotExist()
2121
{
2222
var environment = new Mock<IReactEnvironment>();
2323
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(false);
24-
var component = new ReactComponent(environment.Object, "Foo", "container");
24+
var component = new ReactComponent(environment.Object, null, "Foo", "container");
2525

2626
Assert.Throws<ReactInvalidComponentException>(() =>
2727
{
@@ -34,8 +34,9 @@ public void RenderHtmlShouldCallRenderComponent()
3434
{
3535
var environment = new Mock<IReactEnvironment>();
3636
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
37+
var config = new Mock<IReactSiteConfiguration>();
3738

38-
var component = new ReactComponent(environment.Object, "Foo", "container")
39+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
3940
{
4041
Props = new { hello = "World" }
4142
};
@@ -51,8 +52,9 @@ public void RenderHtmlShouldWrapComponentInDiv()
5152
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
5253
environment.Setup(x => x.Execute<string>(@"React.renderToString(Foo({""hello"":""World""}))"))
5354
.Returns("[HTML]");
55+
var config = new Mock<IReactSiteConfiguration>();
5456

55-
var component = new ReactComponent(environment.Object, "Foo", "container")
57+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
5658
{
5759
Props = new { hello = "World" }
5860
};
@@ -65,8 +67,9 @@ public void RenderHtmlShouldWrapComponentInDiv()
6567
public void RenderJavaScriptShouldCallRenderComponent()
6668
{
6769
var environment = new Mock<IReactEnvironment>();
70+
var config = new Mock<IReactSiteConfiguration>();
6871

69-
var component = new ReactComponent(environment.Object, "Foo", "container")
72+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
7073
{
7174
Props = new { hello = "World" }
7275
};

src/React/IReactSiteConfiguration.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
using Newtonsoft.Json;
1011
using System.Collections.Generic;
1112

1213
namespace React
@@ -42,5 +43,20 @@ public interface IReactSiteConfiguration
4243
/// Specifies whether ES6 (harmony) syntax should be transformed
4344
/// </summary>
4445
IReactSiteConfiguration SetUseHarmony(bool useHarmony);
46+
47+
/// <summary>
48+
/// Gets or sets the configuration for JSON serializer.
49+
/// </summary>
50+
JsonSerializerSettings JsonSerializerSettings { get; set; }
51+
52+
/// <summary>
53+
/// Sets the configuration for json serializer.
54+
/// </summary>
55+
/// <remarks>
56+
/// Thic confiquration is used when component initialization script
57+
/// is being generated server-side.
58+
/// </remarks>
59+
/// <param name="settings">The settings.</param>
60+
IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings);
4561
}
4662
}

src/React/ReactComponent.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class ReactComponent : IReactComponent
3232
/// </summary>
3333
private readonly IReactEnvironment _environment;
3434

35+
/// <summary>
36+
/// Global site configuration
37+
/// </summary>
38+
private readonly IReactSiteConfiguration _configuration;
39+
3540
/// <summary>
3641
/// Name of the component
3742
/// </summary>
@@ -51,12 +56,14 @@ public class ReactComponent : IReactComponent
5156
/// Initializes a new instance of the <see cref="ReactComponent"/> class.
5257
/// </summary>
5358
/// <param name="environment">The environment.</param>
59+
/// <param name="configuration">Site-wide configuration.</param>
5460
/// <param name="componentName">Name of the component.</param>
5561
/// <param name="containerId">The ID of the container DIV for this component</param>
56-
public ReactComponent(IReactEnvironment environment, string componentName, string containerId)
62+
public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration configuration, string componentName, string containerId)
5763
{
5864
EnsureComponentNameValid(componentName);
5965
_environment = environment;
66+
_configuration = configuration;
6067
_componentName = componentName;
6168
_containerId = containerId;
6269
}
@@ -103,7 +110,7 @@ public string RenderJavaScript()
103110
return string.Format(
104111
"React.render({0}, document.getElementById({1}))",
105112
GetComponentInitialiser(),
106-
JsonConvert.SerializeObject(_containerId)
113+
JsonConvert.SerializeObject(_containerId, _configuration.JsonSerializerSettings) // SerializeObject accepts null settings
107114
);
108115
}
109116

@@ -133,7 +140,7 @@ private void EnsureComponentExists()
133140
/// <returns>JavaScript for component initialisation</returns>
134141
private string GetComponentInitialiser()
135142
{
136-
var encodedProps = JsonConvert.SerializeObject(Props);
143+
var encodedProps = JsonConvert.SerializeObject(Props, _configuration.JsonSerializerSettings); // SerializeObject accepts null settings
137144
return string.Format(
138145
"{0}({1})",
139146
_componentName,

src/React/ReactEnvironment.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public IReactComponent CreateComponent<T>(string componentName, T props)
268268
EnsureUserScriptsLoaded();
269269
_maxContainerId++;
270270
var containerId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
271-
var component = new ReactComponent(this, componentName, containerId)
271+
var component = new ReactComponent(this, _config, componentName, containerId)
272272
{
273273
Props = props
274274
};
@@ -412,5 +412,13 @@ private JsRuntimeException WrapJavaScriptRuntimeException(JsRuntimeException ex)
412412
Source = ex.Source,
413413
};
414414
}
415+
416+
/// <summary>
417+
/// Gets the site-wide configuration.
418+
/// </summary>
419+
public IReactSiteConfiguration Configuration
420+
{
421+
get { return _config; }
422+
}
415423
}
416424
}

src/React/ReactSiteConfiguration.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
using Newtonsoft.Json;
1011
using System.Collections.Generic;
1112
using System.Collections.ObjectModel;
1213

@@ -60,7 +61,7 @@ public IList<string> Scripts
6061
/// </summary>
6162
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
6263
public bool UseHarmony { get; set; }
63-
64+
6465
/// <summary>
6566
/// Specifies whether ES6 (harmony) syntax should be transformed
6667
/// </summary>
@@ -69,5 +70,28 @@ public IReactSiteConfiguration SetUseHarmony(bool useHarmony)
6970
UseHarmony = useHarmony;
7071
return this;
7172
}
73+
74+
/// <summary>
75+
/// Gets or sets the configuration for JSON serializer.
76+
/// </summary>
77+
public JsonSerializerSettings JsonSerializerSettings
78+
{
79+
get;
80+
set;
81+
}
82+
83+
/// <summary>
84+
/// Sets the configuration for json serializer.
85+
/// </summary>
86+
/// <param name="settings">Settings.</param>
87+
/// <remarks>
88+
/// Thic confiquration is used when component initialization script
89+
/// is being generated server-side.
90+
/// </remarks>
91+
public IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings)
92+
{
93+
JsonSerializerSettings = settings;
94+
return this;
95+
}
7296
}
7397
}

0 commit comments

Comments
 (0)