Skip to content

Add possibility to set own JsonSerializerSettings in configuration #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/React.Tests/Core/ReactComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void RenderHtmlShouldThrowExceptionIfComponentDoesNotExist()
{
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(false);
var component = new ReactComponent(environment.Object, "Foo", "container");
var component = new ReactComponent(environment.Object, null, "Foo", "container");

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

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

var component = new ReactComponent(environment.Object, "Foo", "container")
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
{
Props = new { hello = "World" }
};
Expand All @@ -65,8 +67,9 @@ public void RenderHtmlShouldWrapComponentInDiv()
public void RenderJavaScriptShouldCallRenderComponent()
{
var environment = new Mock<IReactEnvironment>();
var config = new Mock<IReactSiteConfiguration>();

var component = new ReactComponent(environment.Object, "Foo", "container")
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
{
Props = new { hello = "World" }
};
Expand Down
16 changes: 16 additions & 0 deletions src/React/IReactSiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

using Newtonsoft.Json;
using System.Collections.Generic;

namespace React
Expand Down Expand Up @@ -42,5 +43,20 @@ public interface IReactSiteConfiguration
/// Specifies whether ES6 (harmony) syntax should be transformed
/// </summary>
IReactSiteConfiguration SetUseHarmony(bool useHarmony);

/// <summary>
/// Gets or sets the configuration for JSON serializer.
/// </summary>
JsonSerializerSettings JsonSerializerSettings { get; set; }

/// <summary>
/// Sets the configuration for json serializer.
/// </summary>
/// <remarks>
/// Thic confiquration is used when component initialization script
/// is being generated server-side.
/// </remarks>
/// <param name="settings">The settings.</param>
IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings);
Copy link
Member

Choose a reason for hiding this comment

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

You could just make the property get; set and remove this setter method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll keep the method as I see it's part of a fluent interface

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah, good point! I totally forgot about that.

}
}
13 changes: 10 additions & 3 deletions src/React/ReactComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class ReactComponent : IReactComponent
/// </summary>
private readonly IReactEnvironment _environment;

/// <summary>
/// Global site configuration
/// </summary>
private readonly IReactSiteConfiguration _configuration;

/// <summary>
/// Name of the component
/// </summary>
Expand All @@ -51,12 +56,14 @@ public class ReactComponent : IReactComponent
/// Initializes a new instance of the <see cref="ReactComponent"/> class.
/// </summary>
/// <param name="environment">The environment.</param>
/// <param name="configuration">Site-wide configuration.</param>
/// <param name="componentName">Name of the component.</param>
/// <param name="containerId">The ID of the container DIV for this component</param>
public ReactComponent(IReactEnvironment environment, string componentName, string containerId)
public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration configuration, string componentName, string containerId)
{
EnsureComponentNameValid(componentName);
_environment = environment;
_configuration = configuration;
_componentName = componentName;
_containerId = containerId;
}
Expand Down Expand Up @@ -103,7 +110,7 @@ public string RenderJavaScript()
return string.Format(
"React.render({0}, document.getElementById({1}))",
GetComponentInitialiser(),
JsonConvert.SerializeObject(_containerId)
JsonConvert.SerializeObject(_containerId, _configuration.JsonSerializerSettings) // SerializeObject accepts null settings
);
}

Expand Down Expand Up @@ -133,7 +140,7 @@ private void EnsureComponentExists()
/// <returns>JavaScript for component initialisation</returns>
private string GetComponentInitialiser()
{
var encodedProps = JsonConvert.SerializeObject(Props);
var encodedProps = JsonConvert.SerializeObject(Props, _configuration.JsonSerializerSettings); // SerializeObject accepts null settings
return string.Format(
"{0}({1})",
_componentName,
Expand Down
10 changes: 9 additions & 1 deletion src/React/ReactEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public IReactComponent CreateComponent<T>(string componentName, T props)
EnsureUserScriptsLoaded();
_maxContainerId++;
var containerId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
var component = new ReactComponent(this, componentName, containerId)
var component = new ReactComponent(this, _config, componentName, containerId)
{
Props = props
};
Expand Down Expand Up @@ -412,5 +412,13 @@ private JsRuntimeException WrapJavaScriptRuntimeException(JsRuntimeException ex)
Source = ex.Source,
};
}

/// <summary>
/// Gets the site-wide configuration.
/// </summary>
public IReactSiteConfiguration Configuration
{
get { return _config; }
Copy link
Member

Choose a reason for hiding this comment

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

Remove this property, the config is just injected via the IoC container so ReactEnvironment shouldn't be responsible for exposing it to other code. Any classes that want it should just have it injected into their constructor.

}
}
}
26 changes: 25 additions & 1 deletion src/React/ReactSiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;

Expand Down Expand Up @@ -60,7 +61,7 @@ public IList<string> Scripts
/// </summary>
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
public bool UseHarmony { get; set; }

/// <summary>
/// Specifies whether ES6 (harmony) syntax should be transformed
/// </summary>
Expand All @@ -69,5 +70,28 @@ public IReactSiteConfiguration SetUseHarmony(bool useHarmony)
UseHarmony = useHarmony;
return this;
}

/// <summary>
/// Gets or sets the configuration for JSON serializer.
/// </summary>
public JsonSerializerSettings JsonSerializerSettings
{
get;
set;
}

/// <summary>
/// Sets the configuration for json serializer.
/// </summary>
/// <param name="settings">Settings.</param>
/// <remarks>
/// Thic confiquration is used when component initialization script
/// is being generated server-side.
/// </remarks>
public IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings)
{
JsonSerializerSettings = settings;
return this;
}
}
}