Create [NS]Dictionary from single string in Swift - string

I have a string var dictAsString:String = '["foo" : 123, "bar" : 456]' that I want to convert to a Dictionary (or NSDictionary, I'm not particular.) I've tried
var dictAsObj:AnyObject = dictAsString as AnyObject
var dictAsDict:NSDictionary = dictAsObj as NSDictionary
but that doesn't work. I've also tried
var dictAsDict:NSDictionary = NSDictionary(objectsAndKeys: dictAsString)
and
var dictAsObj:AnyObject = dictAsString as AnyObject
var dictAsDict:NSDictionary = NSDictionary(objectsAndKeys: dictAsObj)
Nothing seems to work, and I can't seem to find any help in the documentation. Any ideas?

That string resembles a JSON object.
You could replace the square brackets with curly brackets and use NSJSONSerialization class to get a dictionary out of it.
Worst case scenario, you should write a little parser.
I suggest using Ragel.
Both tasks are an overkill for a string like that, though.

Related

Passing array as hint in string export?

I'm trying to pass an array as a hint in string export, something like this:
extends Node
var things_list=["thing_1", "thing_2", "thing_3"]
export(String,*things_list) var current_thing=things_list[0]
why am I doing this?
I want to reuse the things_list & to do so I have to tediously write all the same values twice,
once in this list itself & once in export like this:
extends Node
var things_list=["thing_1", "thing_2", "thing_3"]
export(String,"thing_1", "thing_2", "thing_3") var current_thing=things_list[0]
this becomes really annoying with longer lists,
So is there anything like ...array in js
or something like *args in python? (as shown in the first example)
or are the values somehow stored in the export variable itself?
maybe like current_thing.export_hints[1] #thing_2
Those values are stored internally and there is no scripting access. And no, the parser does not understand any syntax for an array in export (nor array spread elsewhere for that matter).
However, there is something similar. You can define an enum:
enum things {thing_1, thing_2, thing_3}
export(things) var current_thing = things.thing_1
As you might remember, if you have an enum, you have a Dictionary. So you can still get the list of things like this:
var things_list = things.keys()
However, the enum will be of int, not of String. Which I don't know if it will be an issue. You can still get the corresponding String like this:
var things_list = things.keys()
print(things_list[current_thing])
And, of course, your other option is to use _get_property_list. It is not overly complicated:
var things_list := ["thing_1", "thing_2", "thing_3"]
var current_thing:String = things_list[0]
func _get_property_list() -> Array:
return [
{
"name": "current_thing",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_ENUM,
"hint_string": PoolStringArray(things_list).join(",")
}
]

Haxe - use string as variable name with DynamicAccess

I am trying to use a string ('npcName') as a variable name. So far I have tried casting dialogMap into a DynamicAccess object, but it gives me the error 'Invalid array access' when I try this:
var npcName:String = 'TestNPC';
var casted = (cast Registry.dialogMap:haxe.DynamicAccess<Dynamic>);
var tempname = casted[root.npcName[0].message];
trace(tempname);
'dialogMap' is an empty map which I want to fill like so:
Registry.dialogMap['message'] = root.npcName[0].message;
How can I use npcName, a string, in the above line of code? Is there a way to transform the string into something usable? Any help would be appreciated.
The haxe.DynamicAccess doesn't have array access (like map[key]), but is an abstract type for working with anonymous structures that are intended to hold collections of objects by the string key. It is designed to work with map.get(key) and map.set(key). It is basically a nicer wrapper around Reflect.field and Reflect.setField and does some safety checks with Reflect.hasField.
var variable = "my_key";
var value = 123;
var dynamicMap = new haxe.DynamicAccess<Dynamic>();
dynamicMap.set(variable, value);
I'm noticing you are doing very much cast and dynamic, so untyped code, which is a bit of contradiction in a typed language. What is the actual type of dialogMap?
Not sure you are aware of it but, Haxe has its own maps, which are fully typed, so you don't need casts.
var map = new Map<String, Int>();
map[variable] = value;
I think this article helps understanding how to work with dynamic (untyped) objects.
Tip; for testing such small functionalities you can doodle around on the try.haxe site : http://try.haxe.org/#4B84E
Hope this helps, otherwise here is some relevant documentation:
http://api.haxe.org/haxe/DynamicAccess.html
https://haxe.org/manual/std-reflection.html
https://haxe.org/manual/types-dynamic.html
http://code.haxe.org/category/beginner/string-variable-reflection.html

Scala how to instantiate new Option[java.sql.Date] type

I have var date_of_birth2 : Option[java.sql.Date] = None.
Later I have to fill date_of_birth2 by string.
String date = "01/01/1990"
var formate = new SimpleDateFormat("dd/MM/yyyy").parse(date)
var aDate = new java.sql.Date(formate.getTime());
But aDate is just the type of java.sql.Date. How can I assign?
var mdate_of_birth2 : Option[java.sql.Date] = date // date is just String type
Instead of explicitly using a Some, as suggested by #hezamu, you could use the apply method on the Option companion like so:
val maybeADate:Option[Date] = Option(aDate)
In this case, in the off chance that aDate was null, then your Option would be a None instead of Some(null) which would be undesirable.
Since the parse method will throw an exception if there is a formatting error, you might also consider using Try, to squash failures to None.
val aDate = Try(new SimpleDateFormat("dd/MM/yyyy").parse(date))
.map(d => new java.sql.Date(d.getTime()))
.toOption
If you do want it to throw an exception, then I would go with #cmbaxter's answer using Option.apply.
You wrap it in Some:
var mdate_of_birth2: Option[java.sql.Date] = Some(aDate)
You should see if you can refactor your code to make it a val, though.

JSON.Net SerializeXnode excluding certain nodes

I have a xml string that i'm trying to convert to JSON using JSON.Net. The problem is that i want only certain part of this xml in my JSON string. Below is the code i use and what i need.
var x = XDocument.Parse(xmlString);
var json = JsonConvert.SerializeXNode(x);
This will convert the whole doc. This is how json string looks like in a JSON Viewer
What i want is ONLY the table (The Arrowed one in image 1) and its descendants to be inside string json.
Is it possible? How to achieve it? Can i use a custom ContractResolver with SerializeXnode?
You've got an XDocument, so why not simply select the part you want and then serialize just that part?
Try something like this:
var doc = XDocument.Parse(xmlString);
var table = doc.XPathSelectElement("//table[#class=\"form\"]");
var json = JsonConvert.SerializeXNode(table);
Note that XPathSelectElement is an extension method, so you will need using System.Xml.XPath; at the top of your code if you don't already have it.
EDIT
You can do it without XPath like this:
var doc = XDocument.Parse(xmlString);
var table = root.Descendants(XName.Get("table"))
.Where(e => e.Attributes(XName.Get("class"))
.Select(a => a.Value)
.FirstOrDefault() == "form")
.First();
var json = JsonConvert.SerializeXNode(table);
Both approaches give the same results, the table plus all descendants.

What is wrong in this LINQ Query, getting compile error

I have a list AllIDs:
List<IAddress> AllIDs = new List<IAddress>();
I want to do substring operation on a member field AddressId based on a character "_".
I am using below LINQ query but getting compilation error:
AllIDs= AllIDs.Where(s => s.AddressId.Length >= s.AddressId.IndexOf("_"))
.Select(s => s.AddressId.Substring(s.AddressId.IndexOf("_")))
.ToList();
Error:
Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<MyCompany.Common.Users.IAddress>'
AllIDs is a list of IAddress but you are selecting a string. The compiler is complaining it cannot convert a List<string> to a List<IAddress>. Did you mean the following instead?
var substrings = AllIDs.Where(...).Select(...).ToList();
If you want to put them back into Address objects (assuming you have an Address class in addition to your IAddress interface), you can do something like this (assuming the constructor for Address is in place):
AllIDs = AllIDs.Where(...).Select(new Address(s.AddressID.Substring(s.AddressID.IndexOf("_")))).ToList();
You should also look at using query syntax for LINQ instead of method syntax, it can clean up and improve the readability of a lot of queries like this. Your original (unmodified) query is roughly equivalent to this:
var substrings = from a in AllIDs
let id = a.AddressId
let idx = id.IndexOf("_")
where id.Length >= idx
select id.Substring(idx);
Though this is really just a style thing, and this compiles to the same thing as the original. One slight difference is that you only have to call String.IndexOf() one per entry, instead of twice per entry. let is your friend.
Maybe this?
var boundable =
from s id in AllIDs
where s.AddressId.Length >= s.AddressId.IndexOf("_")
select new { AddressId = s.AddressId.Substring(s.AddressId.IndexOf("_")) };
boundable = boundable.ToList();

Resources