0

I have a problem with dynamic objects. Here's my script: I have table of content on PostgreSql(version 13). There is a json type field on this table. The interior of this space is completely dynamic. So it doesn't always have the same json structure.

What I want to do is to filter the contents according to the dynamic fields inside the json field. I did this. But what I can't do is: I want to send this content to my razor view. While making this post, I want to convert this json field into a c# object. In other words, it should always return to the razor view as an object that takes this json, with its data in it. Let me give an example.

Id  |  Title            |  DynamicData
-------------------------------------------------------------------
1   |  Content1         | {"Name": "Serkan", "Surname": "Ateş", Details: { "Job": "Programmer", "Age": 24, IsMale: true, Birthday: "2021-08-29 12:54:37" }}
2   |  Content2         | {"Name": "Halil", "Surname": "Kazan", Details: { "Job": "Teacher", "Age": 32, IsMale: true, Birthday: "2022-08-29 12:54:37" }}
3   |  Content3         | {"SlideName": "Blabla", "Order": 1, "ImagePath": "../Images/Slide1.jpg"}
4   |  Content4         | {"SlideName": "Blabla 2", "Order": 3, "ImagePath": "../Images/Slide2.jpg"}

As you can see the structure of my "DynamicData" field is variable. How can I convert the DynamicData field to an object when I pull my data from this table? There may also be hundreds of records in this table. Therefore, performance will be very important for me when converting objects into dynamic objects. According to this table, if I return only 1 and 2 Id records, I want to create a class structure like this and fill its instance with data and return it.


    public class Details
    {
        public string Job { get; set; }
        public int Age { get; set; }
        public bool IsMale { get; set; }
        public string Birthday { get; set; }
    }
    public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public Details Details { get; set; }
    }
    public class Content
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public Person DynamicData { get; set; }
    }

    var contents = helper.GetContents(...);
    //2 Content type record should be returned

I hope I was able to explain my problem. I tried to do it with ExpandoObject but it doesn't quite meet my requirements. I want to take instances of the classes I created at runtime, not dynamically, and send them to the view as a model. Please help me :( Thank you from now.

Edit:

enter image description here

Friends, when I try to do what I want to do with dynamic type, if my dynamic object is an array, I cannot access properties while accessing the elements of that array in a loop. But when I debug, if I watch directly, I can access the values of the object. What is the reason of this?

enter image description here

srkn
  • 45
  • 8
  • How can the converter decide which result class to choose? Is there any implicit logic based on the json content for this? – cly Aug 29 '21 at 20:46
  • There is no implicit logic, I print a form that is created dynamically by the user as json into this field. Think like a form builder. Therefore, we do not know which fields and what types of fields the user will enter. – srkn Aug 29 '21 at 21:10
  • I'm afraid not.You need to create the class before runtime.You can try to use `public class Content { public int Id { get; set; } public string Title { get; set; } }public class Content1:Content { public xxx DynamicData { get; set; } }`to create different Content with different types of `DynamicData` . – Yiyi You Aug 30 '21 at 08:17
  • If we found a magic way which can convert that json content coming from your dynamic form builder how will you know what properties to access on the converted class instances? E.g. when to access ````myConvertedPerson.Name```` property? – cly Aug 30 '21 at 09:25
  • @cly I will mark all fields created on JSon as public. So all fields will be accessible via razor view. I can pull a Json string onto the JSon document and access all fields and subfields through this object. I'm looping through all fields and subfields inside this Json document. – srkn Aug 30 '21 at 11:44
  • ... I'm navigating sub json objects recursively. During this navigation, I create my classes and the properties of these classes together with their types as a public string with "StringBuilder". Then I have all the classes available. I am creating the classes I have in string format with a "System.Reflection.EmitTypeBuilder" class at runtime. After this stage, the only thing left to do is to fill the types I created with the data over the json and get their instances. I couldn't do this part. – srkn Aug 30 '21 at 11:44
  • I suggest to add this information to the question :) – cly Aug 31 '21 at 12:18
  • Did you tried to give more hint for Razor via ````@{((dynamic)slideItem).SiteAya...````? – cly Aug 31 '21 at 12:23

0 Answers0