question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Data from complicated source

See original GitHub issue

Similar to #11, that the data is from deserialized source, but unlike #11, this time, the Json deserialization is not to a unstructured var parsed, but to a structured one. Yet, scriban seems unable to handle such case either.

The data structure and data:

    public class Specs
    {
        public string Storage { get; set; }
        public string Memory { get; set; }
        public string Screensize { get; set; }
    }

    public class Phone
    {
        public string Brand { get; set; }
        public string Type { get; set; }
        public Specs Specs { get; set; }
    }

    public static string json = @"[
          {
            'Brand': 'Nokia','Type' : 'Lumia 800',
            'Specs':{'Storage' : '16GB', 'Memory': '512MB','Screensize' : '3.7'}
          },
          {
            'Brand': 'Nokia', 'Type' : 'Lumia 710',
            'Specs':{'Storage' : '8GB','Memory': '512MB','Screensize' : '3.7'}
          },  
          { 'Brand': 'Nokia','Type' : 'Lumia 900',
            'Specs':{'Storage' : '8GB', 'Memory': '512MB','Screensize' : '4.3' }
          },
          { 'Brand': 'HTC ','Type' : 'Titan II',
            'Specs':{'Storage' : '16GB', 'Memory': '512MB','Screensize' : '4.7' }
          },
          { 'Brand': 'HTC ','Type' : 'Radar',
            'Specs':{'Storage' : '8GB', 'Memory': '512MB','Screensize' : '3.8' }
          }
        ]";

The test code:

        var deserialized =
            JsonConvert.DeserializeObject<List<Phone>>(json);
         Console.WriteLine("\n## Deserialize Phone List");
         Console.WriteLine(JsonConvert.SerializeObject(deserialized));
         Console.WriteLine(deserialized[0].Brand);
         Console.WriteLine(deserialized[1].Specs.Storage);
         //Console.ReadKey();

         var template = Template.Parse(@"{{ phones[0].Brand }}"); // phones[1].Specs.Storage
         var model = new { phones = deserialized };
         var scriptObject = new ScriptObject();
         scriptObject.Import(model);
         // Import the following delegate to scriptObject.myfunction (would be accessible as a global function)
         //scriptObject.Import("serialize", new Func<string>(() => "Hello Func"));

         var context = new TemplateContext();
         context.PushGlobal(scriptObject);
         template.Render(context);
         context.PopGlobal();

The output of phones[0].Brand is empty. If to use the commented out one, phones[1].Specs.Storage, an exception will be thrown. Note that both deserialized[0].Brand) and deserialized[1].Specs.Storage outputs just fine.

Is there any possibility that scriban supports structured data like this? (I was trying to do the serialization myself within the template, passing JsonConvert.SerializeObject as customized function serialize, then using a loop to serialize each entry in List<Phone>.)

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
xoofxcommented, Feb 2, 2017

By default, a TemplateContext has an active IMemberRenamer (through the property TemplateContext.MemberRenamer), and the default renamer setup is StandardMemberRenamer which is basically transforming a camel/pascal case identifier to a lowercase+underscore identifier, meaning that:

  • Title will be translated to title
  • SuperTitle will be translated to super_title

So in your example above, you should have something like:

var template = Template.Parse(@"This {{books[0].title}} {{ ""is"" | string.upcase }} from scriban!");

Note the slight difference: it is books[0].title instead of books[0].Title

All your questions are related to the Runtime document that was unfortunately not written when I finished coding the library. As I see the problems the lack of it can cause, I will try to find some time to start a first draft of this document, as it is obviously needed when using scriban beyond the simple helloworld sample! 😅

The issue tracking this is #4

1reaction
xoofxcommented, Feb 2, 2017

I forgot also that you could also use context.MemberRenamer = new DelegateMemberRenamer(name => name); and it should just work without changing “Title” and “title”. It may also solve the other issue #11

Note that this rename doesn’t affect the renamer used for all the builtins (which is by default the StandardMemberRenamer)

Read more comments on GitHub >

github_iconTop Results From Across the Web

7 Signs You're Dealing with Complex Data
Complex data necessitates additional work to prepare and model the data before it is “ripe” for analysis and visualization. Hence it is important...
Read more >
Complex Data Analysis: Processes and Tools
Complex data analysis uses AI and ML to extract actionable insights from collected data; The data analysis process consists of five steps: definition, ......
Read more >
How do you transform complex data sources?
Learn how to transform complex data sources using common data transformation tools and techniques. Improve your data quality, usability, ...
Read more >
What Makes Complex Data Different
But very often what remains unexploited is complex data. It can be all around us: contracts, lab notes, emails, reports, log files, statements, ......
Read more >
How to Create Effective Summaries from Complex Data ...
What are the best practices for creating concise and informative summaries from complex data sources?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found