-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multilevel mappers, with discriminators in middle classes, and suppor…
…t to abstract and non-abstract middle classes.
- Loading branch information
Showing
18 changed files
with
592 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace FluentJsonNet | ||
{ | ||
public class JsonMapsCache | ||
{ | ||
public JsonMapBase[] Mappers { get; set; } | ||
public JsonMapBase[][] SubMappers { get; set; } | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace FluentJsonNet | ||
{ | ||
public class JsonMapsTree | ||
{ | ||
private readonly Dictionary<Type, JsonMapsTreeNode> dictionary; | ||
|
||
public JsonMapsTree(Dictionary<Type, JsonMapsTreeNode> dictionary) | ||
{ | ||
this.dictionary = dictionary; | ||
} | ||
|
||
public IEnumerable<JsonMapBase> GetMappers(Type objectType) | ||
{ | ||
// find mapper in the dictionary | ||
var current = objectType; | ||
while (current != null) | ||
{ | ||
int count = 0; | ||
JsonMapsTreeNode node; | ||
if (this.dictionary.TryGetValue(current, out node)) | ||
count++; | ||
|
||
var nodes = this.dictionary | ||
.Where(kv => kv.Value.Mapper is IAndSubtypes) | ||
.Where(kv => kv.Value != node) | ||
.Where(kv => kv.Value.Mapper.AcceptsType(current)) | ||
.Select(kv => kv.Value) | ||
.ToList(); | ||
|
||
count += nodes.Count; | ||
|
||
if (count > 1) | ||
throw new Exception($"Ambiguous maps for the type `{current.Name}`"); | ||
|
||
if (nodes.Count > 0) | ||
node = nodes[0]; | ||
|
||
if (node == null) | ||
yield break; | ||
|
||
yield return node.Mapper; | ||
|
||
if (node.Mapper is JsonMap) | ||
yield break; | ||
|
||
current = current.BaseType; | ||
} | ||
} | ||
|
||
public IEnumerable<JsonMapBase[]> GetSubpathes(Type objectType) | ||
{ | ||
JsonMapsTreeNode rootNode; | ||
if (!this.dictionary.TryGetValue(objectType, out rootNode) || rootNode.Children.Count == 0) | ||
yield break; | ||
|
||
var list = new Stack<JsonMapBase>(20); | ||
var stack = new Stack<Queue<JsonMapsTreeNode>>(20); | ||
var node = rootNode; | ||
stack.Push(new Queue<JsonMapsTreeNode>(node.Children.Select(ct => this.dictionary[ct]))); | ||
while (true) | ||
{ | ||
var currentLevel = stack.Peek(); | ||
if (currentLevel.Count > 0) | ||
{ | ||
node = currentLevel.Dequeue(); | ||
stack.Push(new Queue<JsonMapsTreeNode>(node.Children.Select(ct => this.dictionary[ct]))); | ||
list.Push(node.Mapper); | ||
} | ||
else | ||
{ | ||
stack.Pop(); | ||
if (stack.Count == 0) | ||
yield break; | ||
if (list.Peek().CanCreateNew()) | ||
yield return list.ToArray(); | ||
list.Pop(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace FluentJsonNet | ||
{ | ||
public class JsonMapsTreeNode | ||
{ | ||
public JsonMapsTreeNode(JsonMapBase mapper, Type parent, Type[] children) | ||
{ | ||
this.Mapper = mapper; | ||
this.Parent = parent; | ||
this.Children = new ReadOnlyCollection<Type>(children); | ||
} | ||
|
||
public JsonMapBase Mapper { get; } | ||
public Type Parent { get; } | ||
public IReadOnlyCollection<Type> Children { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace FluentJsonNetTests | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] | ||
sealed class NonDefaultTestMapperAttribute : Attribute | ||
{ | ||
public NonDefaultTestMapperAttribute() | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.