Skip to content
Open
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
61 changes: 61 additions & 0 deletions source/Lucene.Net.Linq.Tests/Helpers/JsonTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lucene.Net.Linq.Tests
{
public class JsonTypeConverter<T> : System.ComponentModel.TypeConverter
{
private Type _type = typeof (T);

public static JsonSerializerSettings DefaultSettings { get; set; }

static JsonTypeConverter ()
{
DefaultSettings = new JsonSerializerSettings
{
//DateTimeZoneHandling = DateTimeZoneHandling.Utc,
Formatting = Formatting.None,
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
ObjectCreationHandling = ObjectCreationHandling.Replace
};
}

public override bool CanConvertFrom (System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
{
return true;
}

public override bool CanConvertTo (System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
{
return true;
}

// Overrides the ConvertFrom method of TypeConverter.
public override object ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject ((string)value, _type, DefaultSettings);
}
return Newtonsoft.Json.JsonConvert.SerializeObject (value);
}

// Overrides the ConvertTo method of TypeConverter.
public override object ConvertTo (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value is string)
{
if (destinationType == typeof (string))
return value;
return Newtonsoft.Json.JsonConvert.DeserializeObject ((string)value, destinationType, DefaultSettings);
}
if (destinationType == typeof (string))
return Newtonsoft.Json.JsonConvert.SerializeObject (value, DefaultSettings);
return Newtonsoft.Json.JsonConvert.DeserializeObject (Newtonsoft.Json.JsonConvert.SerializeObject (value), destinationType, DefaultSettings);
}
}
}
55 changes: 55 additions & 0 deletions source/Lucene.Net.Linq.Tests/Integration/SelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,61 @@ public void Where_AnyField_StartsWith()
Assert.That(result.Single().Name, Is.EqualTo("Other Document"));
}

public class SampleWithCollection
{
[Field (Key = true)]
public string Key { get; set; }

[Field (Converter = typeof (HashSetConverter))]
public System.Collections.Generic.HashSet<string> HashSetCollection { get; set; }
}

class HashSetConverter : JsonTypeConverter<System.Collections.Generic.HashSet<string>>
{
}

[Test]
public void StoresAndRetrieves_CollectionWithConverter_HashSet ()
{
var d = new SampleWithCollection { Key = "0", HashSetCollection = new System.Collections.Generic.HashSet<string> { "item 1", "item 2", "item 3" } };
using (var session = provider.OpenSession<SampleWithCollection> ())
{
session.Add (d);
session.Commit ();
}

var documents = provider.AsQueryable<SampleWithCollection> ();

var result = from doc in documents select doc;

var field = result.FirstOrDefault ().HashSetCollection;

Assert.IsTrue (field.Contains ("item 1"));
}

[Test]
public void StoresAndRetrieves_CollectionWithConverter_HashSet_Fluent ()
{
var converter = new JsonTypeConverter<System.Collections.Generic.HashSet<string>> ();
var map = new Lucene.Net.Linq.Fluent.ClassMap<SampleWithCollection> (Lucene.Net.Util.Version.LUCENE_30);
map.Key (x => x.Key);
map.Property (x => x.HashSetCollection).NotIndexed ().ConvertWith (converter);

var d = new SampleWithCollection { Key = "0", HashSetCollection = new System.Collections.Generic.HashSet<string> { "item 1", "item 2", "item 3" } };
using (var session = provider.OpenSession<SampleWithCollection> (map.ToDocumentMapper ()))
{
session.Add (d);
session.Commit ();
}

using (var session = provider.OpenSession<SampleWithCollection> (map.ToDocumentMapper ()))
{
var field = session.Query ().First ().HashSetCollection;

Assert.IsTrue (field.Contains ("item 1"));
}
}

}

}
5 changes: 5 additions & 0 deletions source/Lucene.Net.Linq.Tests/Lucene.Net.Linq.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<Reference Include="Lucene.Net.Contrib.SpellChecker">
<HintPath>..\..\packages\Lucene.Net.Contrib.3.0.3\lib\net40\Lucene.Net.Contrib.SpellChecker.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -106,6 +110,7 @@
<Compile Include="Fluent\ScoreTests.cs" />
<Compile Include="Fluent\StoreTests.cs" />
<Compile Include="Fluent\TermVectorModeTests.cs" />
<Compile Include="Helpers\JsonTypeConverter.cs" />
<Compile Include="Integration\AllowSpecialCharactersTests.cs" />
<Compile Include="Integration\EnumerableContainsTests.cs" />
<Compile Include="Integration\EnumFieldTests.cs" />
Expand Down
1 change: 1 addition & 0 deletions source/Lucene.Net.Linq.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="Common.Logging" version="2.3.1" targetFramework="net40" />
<package id="Lucene.Net" version="3.0.3" targetFramework="net40" />
<package id="Lucene.Net.Contrib" version="3.0.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net40" />
<package id="NUnit" version="2.6.3" targetFramework="net40" />
<package id="Remotion.Linq" version="1.13.183.0" targetFramework="net40" />
<package id="RhinoMocks" version="3.6.1" />
Expand Down
4 changes: 2 additions & 2 deletions source/Lucene.Net.Linq/Fluent/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ protected internal virtual IFieldMapper<T> ToFieldMapper()

Type type;

if (FieldMappingInfoBuilder.IsCollection(propInfo.PropertyType, out type))
if (converter == null && FieldMappingInfoBuilder.IsCollection(propInfo.PropertyType, out type))
{
return new CollectionReflectionFieldMapper<T>(mapper, type);
}
Expand Down Expand Up @@ -293,4 +293,4 @@ private void SetDefaults(PropertyInfo propInfo, PropertyMap<T> copy)
}

}
}
}
7 changes: 7 additions & 0 deletions source/Lucene.Net.Linq/Mapping/FieldMappingInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ internal static IFieldMapper<T> Build<T>(PropertyInfo p, Version version, Analyz

var isCollection = IsCollection(p.PropertyType, out type);

// if a converter is provided, disable collection treatment
if (metadata != null && metadata.Converter != null)
{
type = p.PropertyType;
isCollection = false;
}

ReflectionFieldMapper<T> mapper;

if (numericFieldAttribute != null)
Expand Down