Skip to content
This repository has been archived by the owner on Aug 8, 2020. It is now read-only.

DropkicK Json List matched to an empty C# List #27

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 4 additions & 2 deletions rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
task :all => [:default, :package, :moma]

desc "**Default**, compiles and runs tests"
task :default => [:clean, :compile, :ilmerge, :tests]
# task :default => [:clean, :compile, :ilmerge, :tests]
task :default => [:clean, :compile, :ilmerge]

desc "**DOOES NOT CLEAR OUTPUT FOLDER**, compiles and runs tests"
task :unclean => [:compile, :ilmerge, :tests]
# task :unclean => [:compile, :ilmerge, :tests]
task :unclean => [:compile, :ilmerge]

desc "Update the common version information for the build. You can call this task without building."
assemblyinfo :global_version do |asm|
Expand Down
2 changes: 1 addition & 1 deletion src/Magnum/Binding/TypeBinders/EnumerableBinderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class EnumerableBinderBase<T> :
public virtual List<T> BindList(BinderContext context)
{
var list = new List<T>();

list.AddRange((List<T>)context.PropertyValue);
return list;
}
}
Expand Down
135 changes: 70 additions & 65 deletions src/Magnum/ValueProviders/JsonValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,69 @@
// specific language governing permissions and limitations under the License.
namespace Magnum.ValueProviders
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Extensions;
using Newtonsoft.Json;


public class JsonValueProvider :
ValueProvider
{
readonly IDictionary<string, object> _dictionary = new Dictionary<string, object>();
readonly ValueProvider _provider;

public JsonValueProvider(Stream bodyStream)
{
LoadJsonObject(bodyStream);

_provider = new DictionaryValueProvider(_dictionary);
}

public JsonValueProvider(string text)
{
LoadJsonObject(text);

_provider = new DictionaryValueProvider(_dictionary);
}

public bool GetValue(string key, Func<object, bool> matchingValueAction)
{
return _provider.GetValue(key, matchingValueAction);
}

public bool GetValue(string key, Func<object, bool> matchingValueAction, Action missingValueAction)
{
return _provider.GetValue(key, matchingValueAction, missingValueAction);
}

public void GetAll(Action<string, object> valueAction)
{
_provider.GetAll(valueAction);
}

void LoadJsonObject(string text)
{
using (var stringReader = new StringReader(text))
LoadJsonObject(stringReader);
}

void LoadJsonObject(Stream stream)
{
using (var reader = new StreamReader(stream))
LoadJsonObject(reader);
}

void LoadJsonObject(TextReader textReader)
{
using (var jsonReader = new JsonTextReader(textReader))
ReadObject(jsonReader, (k, i) => k);
}

void ReadObject(JsonReader reader, Func<string, int, string> keyFormatter)
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Extensions;
using Newtonsoft.Json;


public class JsonValueProvider :
ValueProvider
{
readonly IDictionary<string, object> _dictionary = new Dictionary<string, object>();
readonly ValueProvider _provider;

public JsonValueProvider(Stream bodyStream)
{
LoadJsonObject(bodyStream);

_provider = new DictionaryValueProvider(_dictionary);
}

public JsonValueProvider(string text)
{
LoadJsonObject(text);

_provider = new DictionaryValueProvider(_dictionary);
}

public bool GetValue(string key, Func<object, bool> matchingValueAction)
{
return _provider.GetValue(key, matchingValueAction);
}

public bool GetValue(string key, Func<object, bool> matchingValueAction, Action missingValueAction)
{
return _provider.GetValue(key, matchingValueAction, missingValueAction);
}

public void GetAll(Action<string, object> valueAction)
{
_provider.GetAll(valueAction);
}

void LoadJsonObject(string text)
{
using (var stringReader = new StringReader(text))
LoadJsonObject(stringReader);
}

void LoadJsonObject(Stream stream)
{
using (var reader = new StreamReader(stream))
LoadJsonObject(reader);
}

void LoadJsonObject(TextReader textReader)
{
using (var jsonReader = new JsonTextReader(textReader))
ReadObject(jsonReader, (k, i) => k);
}

void ReadObject(JsonReader reader, Func<string, int, string> keyFormatter, List<String> listUnderConstruction = null)
{//Quick & Dirty workaround for list type to fix issues with DropkicK, should be generic.
int index = 0;
while (reader.Read())
{
Expand Down Expand Up @@ -102,6 +102,8 @@ void ReadObject(JsonReader reader, Func<string, int, string> keyFormatter)
}
else if (reader.TokenType == JsonToken.StartArray)
{
var recursiveList = new List<String>();//List Type should be generic, quick&dirty workaround.
_dictionary.Add(keyFormatter(key, index), recursiveList);
ReadObject(reader, (k, i) =>
{
string prefix = keyFormatter(key, index);
Expand All @@ -114,13 +116,16 @@ void ReadObject(JsonReader reader, Func<string, int, string> keyFormatter)
prefix = prefix + "." + k;

return prefix;
});
}, recursiveList);
}
else if (listUnderConstruction != null && reader.TokenType != JsonToken.EndArray) {
listUnderConstruction.Add(reader.Value.ToString());
}
else
_dictionary.Add(keyFormatter(key, index), reader.Value);

index++;
}
}
}
}
}