Question T4855
Visible to All Users

Saving Survey Form Data to MongoDB

created 4 years ago

Is there a standard model class that will allow me to store the Survey to a local MongoDB repository? I'm trying the following:

public partial class FormModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

[JsonProperty("formID", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(ParseStringConverter))]
public long? FormId { get; set; }

[JsonProperty("version", NullValueHandling = NullValueHandling.Ignore)]
public string Version { get; set; }

[JsonProperty("UpdateDate", NullValueHandling = NullValueHandling.Ignore)]
public string UpdateDate { get; set; }

[JsonProperty("UpdateBy", NullValueHandling = NullValueHandling.Ignore)]
public string UpdateBy { get; set; }

[JsonProperty("formName", NullValueHandling = NullValueHandling.Ignore)]
public string FormName { get; set; }

[JsonProperty("formDescription", NullValueHandling = NullValueHandling.Ignore)]
public string FormDescription { get; set; }

[JsonProperty("pages", NullValueHandling = NullValueHandling.Ignore)]
public Page[] Pages { get; set; }
}

public partial class Page
{
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }

[JsonProperty("elements", NullValueHandling = NullValueHandling.Ignore)]
public PageElement[] Elements { get; set; }

[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }

[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
}

public partial class PageElement
{
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
public string Type { get; set; }

[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }

[JsonProperty("html", NullValueHandling = NullValueHandling.Ignore)]
public string Html { get; set; }

[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }

[JsonProperty("hideNumber", NullValueHandling = NullValueHandling.Ignore)]
public bool? HideNumber { get; set; }

[JsonProperty("isRequired", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsRequired { get; set; }

[JsonProperty("startWithNewLine", NullValueHandling = NullValueHandling.Ignore)]
public bool? StartWithNewLine { get; set; }

[JsonProperty("choices", NullValueHandling = NullValueHandling.Ignore)]
public ChoiceUnion[] Choices { get; set; }

[JsonProperty("choicesByUrl", NullValueHandling = NullValueHandling.Ignore)]
public ChoicesByUrl ChoicesByUrl { get; set; }

[JsonProperty("inputType", NullValueHandling = NullValueHandling.Ignore)]
public string InputType { get; set; }

[JsonProperty("max", NullValueHandling = NullValueHandling.Ignore)]
public DateTimeOffset? Max { get; set; }

[JsonProperty("colCount", NullValueHandling = NullValueHandling.Ignore)]
public long? ColCount { get; set; }

[JsonProperty("visibleIf", NullValueHandling = NullValueHandling.Ignore)]
public string VisibleIf { get; set; }

[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }

[JsonProperty("descriptionLocation", NullValueHandling = NullValueHandling.Ignore)]
public string DescriptionLocation { get; set; }

[JsonProperty("titleLocation", NullValueHandling = NullValueHandling.Ignore)]
public string TitleLocation { get; set; }

[JsonProperty("templateElements", NullValueHandling = NullValueHandling.Ignore)]
public TemplateElement[] TemplateElements { get; set; }

[JsonProperty("panelCount", NullValueHandling = NullValueHandling.Ignore)]
public long? PanelCount { get; set; }

[JsonProperty("maxPanelCount", NullValueHandling = NullValueHandling.Ignore)]
public long? MaxPanelCount { get; set; }
}

public partial class ChoiceClass
{
[JsonProperty("value", NullValueHandling = NullValueHandling.Ignore)]
public string Value { get; set; }

[JsonProperty("text", NullValueHandling = NullValueHandling.Ignore)]
public string Text { get; set; }
}

public partial class ChoicesByUrl
{
[JsonProperty("url", NullValueHandling = NullValueHandling.Ignore)]
public Uri Url { get; set; }

[JsonProperty("valueName", NullValueHandling = NullValueHandling.Ignore)]
public string ValueName { get; set; }

[JsonProperty("titleName", NullValueHandling = NullValueHandling.Ignore)]
public string TitleName { get; set; }
}

public partial class TemplateElement
{
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
public TypeEnum? Type { get; set; }

[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }

[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }

[JsonProperty("hideNumber", NullValueHandling = NullValueHandling.Ignore)]
public bool? HideNumber { get; set; }

[JsonProperty("isRequired", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsRequired { get; set; }

[JsonProperty("startWithNewLine", NullValueHandling = NullValueHandling.Ignore)]
public bool? StartWithNewLine { get; set; }

[JsonProperty("elements", NullValueHandling = NullValueHandling.Ignore)]
public TemplateElementElement[] Elements { get; set; }

[JsonProperty("visibleIf", NullValueHandling = NullValueHandling.Ignore)]
public string VisibleIf { get; set; }

[JsonProperty("choices", NullValueHandling = NullValueHandling.Ignore)]
public ChoiceClass[] Choices { get; set; }

[JsonProperty("visible", NullValueHandling = NullValueHandling.Ignore)]
public bool? Visible { get; set; }

[JsonProperty("descriptionLocation", NullValueHandling = NullValueHandling.Ignore)]
public string DescriptionLocation { get; set; }

[JsonProperty("size", NullValueHandling = NullValueHandling.Ignore)]
public long? Size { get; set; }
}

public partial class TemplateElementElement
{
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
public TypeEnum? Type { get; set; }

[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }

[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }

[JsonProperty("hideNumber", NullValueHandling = NullValueHandling.Ignore)]
public bool? HideNumber { get; set; }

[JsonProperty("isRequired", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsRequired { get; set; }

[JsonProperty("elements", NullValueHandling = NullValueHandling.Ignore)]
public ElementElementClass[] Elements { get; set; }

[JsonProperty("visibleIf", NullValueHandling = NullValueHandling.Ignore)]
public string VisibleIf { get; set; }
}

public partial class ElementElementClass
{
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
public TypeEnum? Type { get; set; }

[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }

[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }

[JsonProperty("hideNumber", NullValueHandling = NullValueHandling.Ignore)]
public bool? HideNumber { get; set; }

[JsonProperty("isRequired", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsRequired { get; set; }

[JsonProperty("startWithNewLine", NullValueHandling = NullValueHandling.Ignore)]
public bool? StartWithNewLine { get; set; }

[JsonProperty("choices", NullValueHandling = NullValueHandling.Ignore)]
public string[] Choices { get; set; }

[JsonProperty("choicesByUrl", NullValueHandling = NullValueHandling.Ignore)]
public ChoicesByUrl ChoicesByUrl { get; set; }
}

public enum TypeEnum { Boolean, Dropdown, Panel, Text };

public partial struct ChoiceUnion
{
public ChoiceClass ChoiceClass;
public string String;

public static implicit operator ChoiceUnion(ChoiceClass ChoiceClass) => new ChoiceUnion { ChoiceClass = ChoiceClass };
public static implicit operator ChoiceUnion(string String) => new ChoiceUnion { String = String };
}

internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
ChoiceUnionConverter.Singleton,
TypeEnumConverter.Singleton,
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}

internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);

public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot un-marshal type long");
}

public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}

public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}

internal class ChoiceUnionConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(ChoiceUnion) || t == typeof(ChoiceUnion?);

public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.String:
case JsonToken.Date:
var stringValue = serializer.Deserialize<string>(reader);
return new ChoiceUnion { String = stringValue };
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<ChoiceClass>(reader);
return new ChoiceUnion { ChoiceClass = objectValue };
}
throw new Exception("Cannot un-marshal type ChoiceUnion");
}

public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (ChoiceUnion)untypedValue;
if (value.String != null)
{
serializer.Serialize(writer, value.String);
return;
}
if (value.ChoiceClass != null)
{
serializer.Serialize(writer, value.ChoiceClass);
return;
}
throw new Exception("Cannot marshal type ChoiceUnion");
}

public static readonly ChoiceUnionConverter Singleton = new ChoiceUnionConverter();
}

internal class TypeEnumConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(TypeEnum) || t == typeof(TypeEnum?);

public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
switch (value)
{
case "boolean":
return TypeEnum.Boolean;
case "dropdown":
return TypeEnum.Dropdown;
case "panel":
return TypeEnum.Panel;
case "text":
return TypeEnum.Text;
}
throw new Exception("Cannot un-marshal type TypeEnum");
}

public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (TypeEnum)untypedValue;
switch (value)
{
case TypeEnum.Boolean:
serializer.Serialize(writer, "boolean");
return;
case TypeEnum.Dropdown:
serializer.Serialize(writer, "dropdown");
return;
case TypeEnum.Panel:
serializer.Serialize(writer, "panel");
return;
case TypeEnum.Text:
serializer.Serialize(writer, "text");
return;
}
throw new Exception("Cannot marshal type TypeEnum");
}

public static readonly TypeEnumConverter Singleton = new TypeEnumConverter();
}
}

When I try to save, I get the following: ```
"$.pages[1].elements[4].choices[0]": [
"The JSON value could not be converted to fao.formbuilder.api.Models.ChoiceUnion[]. Path: $.pages[1].elements[4].choices[0] | LineNumber: 0 | BytePositionInLine: 1640."
]

Code
Any help is greatly appreciated

Answers approved by surveyjs Support

created 4 years ago (modified 4 years ago)

Hello,
I would suggest to ask this question on www.stackoverflow.com or forums related to .Net and MongoDB or just google. Your task is to save JSON object into MongoDB using .Net. This task has little to do with SurveyJS Project and with this support forum.

Thank you,
Andrew
SurveyJS Team

    Comments (1)

      Thanks.  The answer, for those searching is json to string

      Get Outlook for iOS