Before deserialize existing .proto file i need to check whether the array property (HtmlCleanerEngineProfile[]) has already satisfied condition. If not, then i should recreate the property.
And here's the problem, after HtmlCleanerEngineProfile[] has been recreated (inside the constructor) this new property still has same value (and length) with old property.
[ProtoContract]
public class HtmlCleanerTemplate : ModelBase
{
private string _templateName;
private int _recursiveLevel = 3;
[ProtoMember(1)]
public string TemplateName
{
get => _templateName;
set => SetProperty(ref _templateName, value);
}
[ProtoMember(2), DefaultValue(3)]
public int RecursiveLevel
{
get => _recursiveLevel;
set => SetProperty(ref _recursiveLevel, value);
}
// This one..
[ProtoMember(3, OverwriteList = true)]
public HtmlCleanerEngineProfile[] EngineProfiles { get; private set; }
public HtmlCleanerTemplate()
{
var engineTypes = RetrieveEngineTypes();
if (EngineProfiles != null && EngineProfiles.Length == engineTypes.Count) return;
// 1. Clone existing to restore checked state
// 2. Recreate HtmlCleanerEngineProfile[]
var tempProfiles = EngineProfiles?.Clone() as HtmlCleanerEngineProfile[];
EngineProfiles = new HtmlCleanerEngineProfile[engineTypes.Count];
for (var i = 0; i < engineTypes.Count; i++)
{
EngineProfiles[i] = new HtmlCleanerEngineProfile
{
EngineName = engineTypes[i].Name,
EngineDescription = ReflectionUtils.GetDescription(engineTypes[i]),
};
// Restore checked state
if (tempProfiles != null && i < tempProfiles.Length)
{
// Todo: if (EngineProfiles[i].EngineName == tempEngines[i].EngineName)
EngineProfiles[i].EngineChecked = tempProfiles[i].EngineChecked;
}
}
}
private static IList<Type> RetrieveEngineTypes()
{
return ReflectionUtils
.GetTypes("ContentManager.Core.Document.Cleaner")
.Where(x => typeof(IHtmlCleanerEngine).IsAssignableFrom(x) && x.Name != typeof(IHtmlCleanerEngine).Name)
.ToList();
}
}
And the HtmlCleanerEnglineProfile
[ProtoContract]
public sealed class HtmlCleanerEngineProfile
{
internal HtmlCleanerEngineProfile() { }
[ProtoMember(1)]
public string EngineName { get; set; }
[ProtoMember(2)]
public string EngineDescription { get; set; }
[ProtoMember(3)]
public bool EngineChecked { get; set; }
}
I've trying to create a new method (same as code inside constructor) and assign [ProtoBeforeDeserialization] attribute. But still i got same result. Did I do something wrong?
Solved by adding [ProtoAfterDeserialization] attribute and [ProtoContract(SkipConstructor = true)].
[ProtoContract(SkipConstructor = true)]
public class HtmlCleanerTemplate : ModelBase
New method
[ProtoAfterDeserialization]
protected void AfterDeserialization()
{
var engineTypes = RetrieveEngineTypes();
if (EngineProfiles != null && EngineProfiles.Length == engineTypes.Count) return;
.......
}
Ctor..
public HtmlCleanerTemplate()
{
AfterDeserialization();
}
Related
After mapping Input to Output, the returned Input must have an incrementing number in its Entries[i].Id, (i.e. Entries[0].Id = 1, Entries[1].Id = 2, Entries[2].Id = 3, ...). So I tried to put it in AfterMapping:
[Mapper]
public interface IMyMapper {
Output Map(Input userTableTemplate);
}
public class RegisterMyMapper : IRegister {
public void Register(TypeAdapterConfig config) {
config.NewConfig<Input, Output>()
.Map(output => output.OutputName, input => input.Name)
.AfterMapping(output => {
foreach (var pair in output.Entries.Select((value, index) => new {value = value, index = index})) {
pair.value.Id = pair.index + 1;
}
});
}
}
public class Output {
public string OutputName { get; set; }
public IEnumerable<Entry> Entries { get; set; }
}
public class Entry { public int Id { get; set; } }
public class Input { public string Name { get; set; } }
But when running _myMapper.Map(myInput) I'm getting a null reference exception, because the private Action<Output> Action1 member of the generated code (public partial class MyMapper : IIMyMapper) is null:
public partial class MyMapper : IMyMapper
{
private Action<Output> Action1;
public Output Map(Input p1)
{
if (p1 == null)
{
return null;
}
Output result = new Output();
result.OutputName = p1.Name;
Action1.Invoke(result);
return result;
}
}
Below are my classes
public class CommonClassSource
{
public int Id {get;set;}
}
public class CommonClassDestination
{
public int Id {get;set;}
}
public class SourceClass
{
public CommonClassSource CommonSource {get;set;}
}
public class DestinationClass
{
public CommonClassDestination CommonDestination {get;set;}
}
Is there any way to add null-checking in config.Bind? For example: here, check if source.CommonSource is null
TinyMapper.Bind<SourceClass, DestinationClass>(config => {
config.Bind(source => source.CommonSource.Id,
target => target.CommonDestination.Id));
});
If all you wanted to have the case handled that it there isn't an issue when source.CommonSource is null, the following script should work for you.
static bool isSourceNull;
public static void Main()
{
Func<SourceClass,object> getSCI = GetSourceClassId;
Func<DestinationClass, object> getDCI = GetDestClassId;
TinyMapper.Bind<SourceClass, DestinationClass>(config => {
config.Bind(source=>getSCI,
target =>getDCI);
});
}
static object GetSourceClassId(SourceClass source)
{
isSourceNull = source.CommonSource == null;
if (isSourceNull) return -1;
else return source.CommonSource.Id;
}
static object GetDestClassId(DestinationClass destinationClass)
{
if (isSourceNull)
{
destinationClass.CommonDestination = null;
return -1;
}
else if (destinationClass.CommonDestination != null)
return destinationClass.CommonDestination.Id;
else return -1;
}
I have a class that contains Range[] as property and Range class is a self referencing class. I used [JsonIgnore] to prevent StackoverflowException but it works for only Serialize not Deserialize. How can I fix this?
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace testoverflow
{
class Program
{
public static void Main(string[] args)
{
GlobalVariable.Json = "[{\"TotalBytesReceived\":0,\"Id\":\"b03750fb291a46708f8e1a7409553075\",\"NofThread\":8,\"Speed\":0,\"Progress\":0.0,\"FilePath\":\"C:\\\\Users\\\\kafeinaltor\\\\Downloads\",\"RangeDir\":\"C:\\\\Users\\\\kafeinaltor\\\\AppData\\\\Roaming\",\"Url\":\"http://ipv4.download.thinkbroadband.com/20MB.zip\",\"Ranges\":[{\"Start\":0,\"End\":9223372036854775806,\"TotalBytesReceived\":0,\"IsDownloaded\":false,\"FileId\":\"87cd7715dc0740c1b82ddd681bf2523d\",\"Size\":9223372036854775807,\"Status\":4,\"IsIdle\":false,\"SaveDir\":\"C:\\\\Users\\\\kafeinaltor\\\\AppData\\\\Roaming\",\"FilePath\":\"C:\\\\Users\\\\kafeinaltor\\\\AppData\\\\Roaming\\\\87cd7715dc0740c1b82ddd681bf2523d\",\"Md5Checksum\":null}],\"Info\":null,\"DownloadRequestMessage\":null}]";
var a = new MTDO();
Console.WriteLine(GlobalVariable.Json);
Console.ReadKey(true);
}
public static class GlobalVariable
{
public static string Json { get; set; }
}
public class MTDO
{
public MTDO()
{
Ranges = new Range[]
{
new Range(0L, 100L, ""),
new Range(101L, 200L, "")
};
Id = Guid.NewGuid().ToString("N");
Reminder.AddOrUpdate(this);
}
public string Id { get; set; }
public Range[] Ranges{ get; set; }
}
public class Range
{
public long Start { get; set; }
public long End { get; set; }
public string SaveDir { get; set; }
public long TotalBytesReceived{ get; set; }
public Range(long start, long end, string saveDir)
{
this.Start = start;
this.End = end;
this.SaveDir = Guid.NewGuid().ToString();
}
[JsonIgnore]
public Range Remaining
{
get
{
return new Range(Start + TotalBytesReceived, End, SaveDir);
}
}
}
public class Reminder
{
public Reminder()
{
}
public static void AddOrUpdate(MTDO mtdo)
{
var list = JsonConvert.DeserializeObject<List<MTDO>>(Read());
if (list == null)
list = new List<MTDO>();
var exists = list.Any(x => x.Id == mtdo.Id);
if (!exists)
list.Add(mtdo);
else
{
var i = list.Select((x, j) => new {val = x, index = j})
.First(x => x.val.Id == mtdo.Id).index;
list[i] = mtdo;
}
WriteJson(list);
}
public static List<MTDO> ReadList()
{
var list = JsonConvert.DeserializeObject<List<MTDO>>(Read());
if (list == null)
list = new List<MTDO>();
return list;
}
static string Read()
{
try
{
return GlobalVariable.Json;
}
catch
{
return "";
}
}
static void WriteJson(List<MTDO> list)
{
GlobalVariable.Json = JsonConvert.SerializeObject(list);
}
}
}
}
UPDATE: I have updated myquestion adding minimum reproducable code in Console Application. You can copy/paste and run directly.
The problem is that you have an infinite recursion:
You call MTDO constructor
Inside MTDO constructor you call Reminder.AddOrUpdate(this);
Inside that method you have var list = JsonConvert.DeserializeObject<List<MTDO>>(Read());
Which calls MTDO constructor again (step 1)
These steps keep repeating until you get StackOverflowException.
When I run the code, it returns -1, although the object (in my understanding) has to be there, since it's being produced by the foreach statement.
Does anyone have an idea?
public partial class Class1
{
private List<Master_Menu_Item> Config
{
get { return JsonConvert.DeserializeObject<List<Master_Menu_Item>>(Json_string.Config); } //if you need an example json, i can provide it
}
private void Reload_Master_stack()
{
if (Config != null)
{
foreach (Master_Menu_Item master_Menu_Item in Config)
{
int index = Config.IndexOf(master_Menu_Item);
Debug.WriteLine(index);
}
}
}
}
class Master_Menu_Item
{
public string Name { get; set; }
public string Icon_path { get; set; }
public List<Bottom_Menu_Item> Bottom_Menu_Items { get; set; }
}
You can use below code:
if (Config != null)
{
foreach (Master_Menu_Item master_Menu_Item in Config)
{
int index = Config.FindIndex(a => a.Id == Master_Menu_Item.Id );
}
}
I have some problem with Emit mapper when I try to save in database properties.
In first I mapped this class (it work good):
[Serializable]
public class ProfileProperty
{
public string PropertyValue { get; set; }
public bool IsVisible { get; set; }
public ProfileProperty(string value, bool isVisible = true)
{
this.PropertyValue = value;
this.IsVisible = isVisible;
}
public ProfileProperty()
{
this.IsVisible = true;
}
}
I mapped here:
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PollmericaProfile, ProfileModel>();
ProfileModel prof = new ProfileModel();
if (x.User != null)
{
prof = mapper.Map(x);
}
But some of the requirements need not a string property. That's why I decided to write this:
[Serializable]
public class ProfileProperty
{
public object PropertyValue { get; set; }
public bool IsVisible { get; set; }
public ProfileProperty(object value, bool isVisible = true)
{
this.PropertyValue = value;
this.IsVisible = isVisible;
}
public ProfileProperty()
{
this.IsVisible = true;
}
public T GetValue<T>()
{
return (T)this.PropertyValue;
}
}
And all mapping is not worked =(
If you ccan, help me please. If you want I can provide the necessary information.
P.S. To be honest, I want to transfer to a string and back, so at least works
UPD: I tried without method public T GetValue<T>()... It works...
Sorry for this, but I find answer very quicly.
in mapping I must to write this:
var mapper = ObjectMapperManager
.DefaultInstance
.GetMapper<PollmericaProfile, ProfileModel>( new DefaultMapConfig()
.IgnoreMembers<PollmericaProfile, ProfileModel>(new string[] { "GetValue" }));
ProfileModel prof = new ProfileModel();
if (x.User != null)
{
prof = mapper.Map(x);
}