[go: nahoru, domu]

Skip to content

Commit

Permalink
Read-me with NamingConventions
Browse files Browse the repository at this point in the history
  • Loading branch information
masbicudo committed Jul 26, 2016
1 parent ce1f0fe commit ba70c8a
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<Compile Include="JsonMapsTree.cs" />
<Compile Include="JsonMapsTreeNode.cs" />
<Compile Include="JsonSubclassMap.cs" />
<Compile Include="Utils\NamingStrategies.cs" />
<Compile Include="Utils\NamingConventions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\EnumExt.cs" />
</ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions Fluent-Json.NET-Configuration/JsonMapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void Map(this JsonMapBase jsonMap, LambdaExpression expr, string n
});
}

public static void NamingStrategy<T>(this JsonMap<T> jsonMap, Func<string, string> renamer)
public static void NamingConvention<T>(this JsonMap<T> jsonMap, Func<string, string> renamer)
{
jsonMap.Actions.Add((member, property, mode) =>
{
Expand All @@ -111,5 +111,4 @@ public static void NamingStrategy<T>(this JsonMap<T> jsonMap, Func<string, strin
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FluentJsonNet.Utils
{
public static class NamingStrategies
public static class NamingConventions
{
public static string CamelCase(string name)
{
Expand Down
5 changes: 3 additions & 2 deletions FluentJsonNetTests/Maps/ComplexNamesMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public ComplexNamesMap()
{
this.DiscriminateSubClassesOnField("type");
this.DiscriminatorValue(this.DiscriminateType);
this.NamingStrategy(NamingStrategies.CamelCase);

this.NamingConvention(NamingConventions.CamelCase);
}

private string DiscriminateType(Type arg)
Expand All @@ -19,7 +20,7 @@ private string DiscriminateType(Type arg)
return "subtype1";
if (arg == typeof(ComplexNamesBase))
return "base";
throw new NotImplementedException();
throw new NotSupportedException();
}
}
}
2 changes: 1 addition & 1 deletion FluentJsonNetTests/Maps/ComplexNamesSingleMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ComplexNamesSingleMap : JsonMap<ComplexNamesSingle>
{
public ComplexNamesSingleMap()
{
this.NamingStrategy(NamingStrategies.CamelCase);
this.NamingConvention(NamingConventions.CamelCase);
}
}
}
28 changes: 14 additions & 14 deletions FluentJsonNetTests/NamingStrategiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,98 +10,98 @@ public class NamingStrategiesTests
[TestMethod]
public void Test_NamingStrategies_Sep1()
{
var names = NamingStrategies.SeparateNames("MiguelAngeloSantosBicudo").ToArray();
var names = NamingConventions.SeparateNames("MiguelAngeloSantosBicudo").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "Miguel", "Angelo", "Santos", "Bicudo" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep2()
{
var names = NamingStrategies.SeparateNames("Miguel Angelo Santos Bicudo").ToArray();
var names = NamingConventions.SeparateNames("Miguel Angelo Santos Bicudo").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "Miguel", "Angelo", "Santos", "Bicudo" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep3()
{
var names = NamingStrategies.SeparateNames("MIGUEL_ANGELO_SANTOS_BICUDO").ToArray();
var names = NamingConventions.SeparateNames("MIGUEL_ANGELO_SANTOS_BICUDO").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "MIGUEL", "ANGELO", "SANTOS", "BICUDO" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep4()
{
var names = NamingStrategies.SeparateNames("MIGUEL-ANGELO_SANTOS BICUDO").ToArray();
var names = NamingConventions.SeparateNames("MIGUEL-ANGELO_SANTOS BICUDO").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "MIGUEL", "ANGELO", "SANTOS", "BICUDO" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep5()
{
var names = NamingStrategies.SeparateNames("miguel-angelo-santos-bicudo").ToArray();
var names = NamingConventions.SeparateNames("miguel-angelo-santos-bicudo").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "miguel", "angelo", "santos", "bicudo" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep6()
{
var names = NamingStrategies.SeparateNames("miguel-angelo_santos bicudo").ToArray();
var names = NamingConventions.SeparateNames("miguel-angelo_santos bicudo").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "miguel", "angelo", "santos", "bicudo" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep7()
{
var names = NamingStrategies.SeparateNames("miguelAngelo_Santos BICUDO").ToArray();
var names = NamingConventions.SeparateNames("miguelAngelo_Santos BICUDO").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "miguel", "Angelo", "Santos", "BICUDO" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep8()
{
var names = NamingStrategies.SeparateNames("_miguelAngelo_Santos BICUDO ").ToArray();
var names = NamingConventions.SeparateNames("_miguelAngelo_Santos BICUDO ").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "miguel", "Angelo", "Santos", "BICUDO" }));
}

[TestMethod]
public void Test_NamingStrategies_Sep9()
{
var names = NamingStrategies.SeparateNames("_ -_miguelAngelo_ _Santos-- BICUDO ").ToArray();
var names = NamingConventions.SeparateNames("_ -_miguelAngelo_ _Santos-- BICUDO ").ToArray();
Assert.IsTrue(names.SequenceEqual(new[] { "miguel", "Angelo", "Santos", "BICUDO" }));
}

[TestMethod]
public void Test_NamingStrategies_UpperCamelCase()
{
var name = NamingStrategies.UpperCamelCase("_ -_miguelAngelo_ _Santos-- BICUDO ");
var name = NamingConventions.UpperCamelCase("_ -_miguelAngelo_ _Santos-- BICUDO ");
Assert.AreEqual("MiguelAngeloSantosBicudo", name);
}

[TestMethod]
public void Test_NamingStrategies_AllUpper()
{
var name = NamingStrategies.AllUpperUnderscoreSeparated("_ -_miguelAngelo_ _Santos-- BICUDO ");
var name = NamingConventions.AllUpperUnderscoreSeparated("_ -_miguelAngelo_ _Santos-- BICUDO ");
Assert.AreEqual("MIGUEL_ANGELO_SANTOS_BICUDO", name);
}

[TestMethod]
public void Test_NamingStrategies_AllLower()
{
var name = NamingStrategies.AllLowerHyphenSeparated("_ -_miguelAngelo_ _Santos-- BICUDO ");
var name = NamingConventions.AllLowerHyphenSeparated("_ -_miguelAngelo_ _Santos-- BICUDO ");
Assert.AreEqual("miguel-angelo-santos-bicudo", name);
}

[TestMethod]
public void Test_FirstToUpper()
{
var name = NamingStrategies.FirstToUpperInvariant("miguel");
var name = NamingConventions.FirstToUpperInvariant("miguel");
Assert.AreEqual("Miguel", name);
}

[TestMethod]
public void Test_FirstToUpper2()
{
var name = NamingStrategies.FirstToUpperInvariant("MIGUEL");
var name = NamingConventions.FirstToUpperInvariant("MIGUEL");
Assert.AreEqual("Miguel", name);
}
}
Expand Down
175 changes: 123 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,93 +1,164 @@
# Fluent-Json.NET
Fluent configurations for Json.NET, allows you to map objects, use type discriminators, all without interfering with your data objects. No attributes are required.
Fluent configurations for Json.NET, allows you to map objects,
use type discriminators, all without interfering with your data objects.
No attributes are required. You can even setup serialization of
third party library types.

# NuGet

Install-Package Fluent-Json.NET

# Example
# Examples

**serializing a Lion class that inherits from Animal, with a discriminator field**
These examples are extracted from test cases... with one or two lines less for simplicity.

{
var jsonStr = JsonConvert.SerializeObject(new Lion(45f, 200f, 1000f));
Assert.AreEqual("{\"strength\":1000.0,\"sight\":200.0,\"speed\":45.0,\"class\":\"lion\"}", jsonStr);
}
- Plugging fluent classes to Json.NET
- Serializing with a discriminator field
- Camel case naming

**Plugging fluent classes to Json.NET**
Plugging fluent classes to Json.NET
-----------------------------------

{
JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(this.GetType().Assembly.GetTypes());
}
JsonConvert.DefaultSettings = JsonMaps.GetDefaultSettings(this.GetType().Assembly.GetTypes());

**Model classes**
Serializing with a discriminator field
--------------------------------------

Discriminator fields provide additional data to the seriaized object,
so that the deserialization process can then tell what is the type of that object.

- **Code**

var jsonStr = JsonConvert.SerializeObject(new Lion(45f, 200f, 1000f));

- **Resulting JSON**

public class Animal
{
public Animal()
{
"strength": 1000.0,
"sight": 200.0,
"speed": 45.0,
"class": "lion" // <-- this is the discriminator field
}

public Animal(float speed)
- **Mapping classes**

public class AnimalMap : JsonMap<Animal>
{
this.Speed = speed;
public AnimalMap()
{
this.DiscriminateSubClassesOnField("class");
this.Map(x => x.Speed, "speed");
}
}

public float Speed { get; set; }
}

public abstract class Feline : Animal
{
protected Feline()
public class FelineMap : JsonSubclassMap<Feline>
{
public FelineMap()
{
this.Map(x => x.SightRange, "sight");
}
}

protected Feline(float speed, float sightRange) : base(speed)
public class LionMap : JsonSubclassMap<Lion>
{
this.SightRange = sightRange;
public LionMap()
{
this.DiscriminatorValue("lion");
this.Map(x => x.Strength, "strength");
}
}

public float SightRange { get; set; }
}
- **Model classes**

public class Lion : Feline
{
public Lion()
public class Animal
{
public Animal(float speed)
{
this.Speed = speed;
}

public float Speed { get; set; }
}

public Lion(float speed, float sightRange, float strength) : base(speed, sightRange)
public abstract class Feline : Animal
{
this.Strength = strength;
protected Feline(float speed, float sightRange) : base(speed)
{
this.SightRange = sightRange;
}

public float SightRange { get; set; }
}

public float Strength { get; set; }
}
public class Lion : Feline
{
public Lion(float speed, float sightRange, float strength) : base(speed, sightRange)
{
this.Strength = strength;
}

public float Strength { get; set; }
}
Camel case naming
-----------------

**Mapping classes**
Naming strategies can be defined so that there is no need to map each field.
Just use the `NamingStrategy` method to define one.

public class AnimalMap : JsonMap<Animal>
{
public AnimalMap()
- **Code**

var jsonStr = JsonConvert.SerializeObject(new ComplexNamesSubclass("Miguel", "Bicudo"));

- **Resulting JSON**

{
"nameInSubclass": "Miguel",
"nameInBase": "Bicudo",
"type": "subtype1"
}

- **Mapping classes**

public class ComplexNamesMap : JsonMap<ComplexNamesBase>.AndSubtypes
{
this.DiscriminateSubClassesOnField("class");
this.Map(x => x.Speed, "speed");
public ComplexNamesMap()
{
this.DiscriminateSubClassesOnField("type");
this.DiscriminatorValue(this.DiscriminateType);

this.NamingStrategy(NamingStrategies.CamelCase);
}

private string DiscriminateType(Type arg)
{
if (arg == typeof(ComplexNamesSubclass))
return "subtype1";
if (arg == typeof(ComplexNamesBase))
return "base";
throw new NotSupportedException();
}
}
}

public class FelineMap : JsonSubclassMap<Feline>
{
public FelineMap()
- **Model classes**

public class ComplexNamesSubclass : ComplexNamesBase
{
this.Map(x => x.SightRange, "sight");
public ComplexNamesSubclass(string nameOfTheEntity, string baseName)
: base(baseName)
{
this.NameInSubclass = nameOfTheEntity;
}

public string NameInSubclass { get; set; }
}
}

public class LionMap : JsonSubclassMap<Lion>
{
public LionMap()
public class ComplexNamesBase
{
this.DiscriminatorValue("lion");
this.Map(x => x.Strength, "strength");
public ComplexNamesBase(string nameOfTheEntity)
{
this.NameInBase = nameOfTheEntity;
}

public string NameInBase { get; set; }
}
}

0 comments on commit ba70c8a

Please sign in to comment.