Data from complicated source
See original GitHub issueSimilar 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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
By default, a
TemplateContext
has an activeIMemberRenamer
(through the propertyTemplateContext.MemberRenamer
), and the default renamer setup isStandardMemberRenamer
which is basically transforming a camel/pascal case identifier to a lowercase+underscore identifier, meaning that:Title
will be translated totitle
SuperTitle
will be translated tosuper_title
So in your example above, you should have something like:
Note the slight difference: it is
books[0].title
instead ofbooks[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
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 #11Note that this rename doesn’t affect the renamer used for all the builtins (which is by default the
StandardMemberRenamer
)