how to convert lambda expressions into json string - c#

I have two applications. One application saves options and configurations down as JSON and the other reads the JSON and performs it's task based on the fields in the JSON. Now I want to filter a list that is in application-2. How can I pass how I want the list to be filtered into a string to be stored in JSON and then reinterpreted by application-2?
Is there anyway to serialize linq/lambda expressions and deserialize them? Or is there a better approach like creating a class that contains some filterable options like equal-to, not-equal-to, greater-than, less-than, contains, etc?

Unfortunately there is no way to serialize and serialize a lamda expression in c#, because it is make in compile time.
The Lamda after the compilation generates a function and the compiler call this function when the lamda expression is used.
You have one option, but is not easy :) Yoy have to store a c# code in json file, and the application-2 will read it, parse it, compile it, and execute it.
But itt will be a complete assembly (like one class) not only one lamdba expression.
If you use :net framrwork here is an example:
https://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime
If you use .Net Core, teh ypu have to use roslyn: https://josephwoodward.co.uk/2016/12/in-memory-c-sharp-compilation-using-roslyn
I hope it helps
regrads

I found for another solution for this problem. You can store and revert an Expression from string! :)
The only need, the two module (or two program in your case) must be the known the same type.
Sample code:
var discountFilter = "album => album.Quantity > 0";
var options = ScriptOptions.Default.AddReferences(typeof(Album).Assembly);
Func<Album, bool> discountFilterExpression =
await CSharpScript.EvaluateAsync<Func<Album, bool>>(discountFilter, options);
var discountedAlbums = albums.Where(discountFilterExpression);
Regrads
gy

If you want to filter javascript arrays in pure JSON, you can use Jpath and Json.Net
For example:
var token = JToken.Parse("json string here")
var tokens = token.SelectTokens("$.YourJsonArray[?(#.Property == something)]")

Related

Changing json query delimiter with Json.net

I'm working with Newtonsoft Json and in order to query a json string, I use the SelectToken method which uses "." as a delimiter to query the hierarchy of the json as described in the docks:
https://www.newtonsoft.com/json/help/html/SelectToken.htm
Is there some way to change the delimiter from using "." to using ":"? meaning that instead of querying for example "a.b.c", I could query for "a:b:c"? It doesn't have to use the SelectToken, but I still got to have a way to reach a specific path with given keys (like the a,b,c in my example above)
No, there is no way to change it. The argument provided to SelectToken is a path which Json.Net internally parses and converts to JPath. If you check the source for same, you can see . is hardcoded as one of the switch-cases in ParsePath method of JPath class. Check here
and here too.

How to simplify an expression when converting it to a string?

I need to pass an expression from a WinForms client to a WebApi. I have the following expression:
var results = somelist.Select(p => p.Id).ToList<int>();
Expression<Func<MyObj, bool>> myexp = x => results.Contains(x.Id);
I then simply did:
var str = myexp.Body.ToString();
However, in this particular example, the expression body looks like this:
value(MyApp.MyForm+<>c__DisplayClass41_0).results.Contains(x.Id)
Which obviously will not work when translating back the string to an expression server-side.
Is there a way to reduce, compile, whatever the expression in a better way? Or should I use 3rd party solutions like Remote.Linq or Serialize.Linq?
It depends on what you need to do with the expression on the server side.
If you really only need a string representation you might want to implement an ExpressionVisitor. However, this may require quite some effort, depending the kind of expressions you need to support.
In case you want to convert back to a proper expression tree and execute the expression on server side, it's definitely worth the have a look at Remote.Linq.

How to deserialize in C# an unknown JSON string to some Object

I need to parse in C# (key ,value wise) a string that is built in a JSON format (to be exact I need to parse the binding parameter of Knockout data-bind).
I go over the html file and I extract the bindings. I want to modify each and every binding (string-wise), but It's really hard for me to parse the string, since I can't really know where each binding stops and the other starts.
for example:
data-bind="text:'ggggg',event:{mouseover:x=function(){alert(1);return 'd,y'}}"
will result in the following string:
"text:'ggggg',event:{mouseover:x=function(){alert(1);return 'd,y'}}"
I want to modify the string in the following way:
newString= "text('gggg'),event(mouseover(x=function(){alert(1);return 'd,y'}))"
I figured out that the best way to do it is to deserialize the string by JSON and then it will be easier for me to get access to each and every binding element.
I write at C#, but since I go over the html file and each data-bind is different and can contain different amount and type of attributes I would like to have a general object that I can deserialize to.
I checked out DataContractJsonSerializer but I don't see how it solves my problem.
Can you please suggest me what's best for my case?
Mary
You can do it with something like this:
var obj = ko.bindingProvider.instance.getBindings(yourDomElement,
ko.contextFor(yourDomElement));
alert(JSON.stringify(obj));
And then do whatever you want with obj.
Fiddle
But... well... don't!

F# Xml TypeProvider for C#

Is there an equivalent to the F# TypeProvider in C#? I am looking at the easiest way to read an Xml file with C#. At this point I am planning to use XDocument, but I was wondering if there was something better.
F# makes it so easy to read an Xml file that I wonder if I shouldn't switch language for Xml processing!
As already mentioned, C# does not support type providers and there is no easy way for emulating them (as Gustavo mentioned, you could use generated type providers through a small F# library, but XML, JSON, CSV and others are not this kind.)
I think using F# library that does the processing is the best approach.
A more complicated alternative would be to write code that lets you define types to model the XML file in C# and then automatically reads the file into these classes. I do not think anything like this exists, but I wrote a similar thing in F# (before type providers), so you can see the snippet for inspiration. The usage looks like this:
// Modelling RSS feeds - the following types define the structure of the
// expected XML file. The StructuralXml parser can automatically read
// XML file into a structure formed by these types.
type Title = Title of string
type Link = Link of string
type Description = Description of string
type Item = Item of Title * Link * Description
type Channel = Channel of Title * Link * Description * list<Item>
type Rss = Rss of Channel
// Load data and specify that names in XML are all lowercase
let url = "http://feeds.guardian.co.uk/theguardian/world/rss"
let doc : StructuralXml<Rss> =
StructuralXml.Load(url, LowerCase = true)
// Match the data against a type modeling the RSS feed structure
let (Rss(channel)) = doc.Root
let (Channel(_, _, _, items)) = channel
for (Item(Title t, _, _)) in items do
printfn "%s" t
Is there any reason why you can't add an F# library to your solution? That project can create the abstraction over the XML that you want, and you could consume it from your C# projects.
No, it's not possible to use type providers directly in C#.
For generated type providers, you can "instantiate" the type provider in a F# project, and then use that from C#, but that doesn't work for erased type providers like CsvProvider, JsonProvider, XmlProvider, Freebase or Worldbank. (It will work for the builtin SqlProvider and WsdlProvider, though).
There has been some discussion of switching CsvProvider, JsonProvider and XmlProvider to the generated model so they're usable for data-binding in C# (https://github.com/fsharp/FSharp.Data/issues/134#issuecomment-20104995), but that will probably take a while to happen.
My suggestion is to either swtich to F# altogether, or create record types that mirror the types that the type provider creates. Example:
type T = {
A : int
B : string
(...)
}
type XmlT = XmlProvider<"xml file">
let getXml() =
let xml = XmlT.GetSample()
{ A = xml.A
B = xml.B
(...) }
I took a look at your link to the XmlProvider in F# and I have not seen anything like it in C#. I use C# with Xml quite alot and often use XDocument and XElement to load and parse Xml with. Once I have Xml loaded into the XDocument I use Linq to Xml to perform similar things. I often use Linq Pad for general queries into the Xml which works well with C# not sure about F# though.

How to convert a search string to a boolean condition?

I have a search criteria stored in a string:
string Searchstr = "(r.Value.Contains("PwC") || (r.Value.Contains("Canadian") && r.Value.Contains("thrive"))) || (r.Value.Contains("Banana") && r.Value.Contains("Gayle"))"
I want to use this in a If statement to check the values:
if(searchstr)
{
then do this....
}
but the if should have a searchstr as boolean.
How to convert this to boolean?
EDIT: The requirement is to give search criteria dynamically in a text box in the following format - "PwC OR (Canadian AND thrive)".
Which will be used to search an XML file.
Therefore I have loaded an XML file and want to have a Where condition in LINQ for which I need to use Dynamic LINQ but string is not allowed in that and also I have some braces to deal with.
Thinking of that I have taken the resultset from the XML(The tag value which i need to search)
var selectedBook = from r in document.Root.Descendants("Archives").Elements("Headline").Elements("Para")
select r;
and would ideally like to try something like:
var query=selectedbook.Where(searchstr)
OR
if(searchstr){....then do this}
You will need to do a bit of work to make this happen, but it is possible.
You should have a look at the dynamic LINQ library. This allows you to specify LINQ conditions (and other clauses) as strings and execute them just like LINQ operators.
Start with the explanation on ScottGu's blog and follow the links:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
I'm assuming the string is going to reference only a specific set of objects (r or r.Value in this case, for example - or anything else you want, as long as you know it beforehand). If this is the case, then:
Create a delegate that takes the objects (that may be referenced) as parameters
and returns a bool, as you want.
Programmatically write a small C# source file in memory that defines the query
as the body of a method (with a fixed name, preferably) that conforms to the delegate specified above.
Use the CSharpCodeProvider class to compile an assembly
with your custom function that returns the bool you want.
Run the dynamically written and compiled code from your main program.
Well as you may guess it is not going to be straight forward but at the same time it is not as hard a problem as it seems
You can perform a few steps to get what you want:
Get the search expression as input (for e.g. "PwC OR (Canadian AND thrive)")
Write an extension method on XElement that returns true and takes the search criteria as input. You will then be able to use
var selectedBook = from r in
document.Root.Descendants("Archives").Elements("Headline").Elements("Para")
where r.SatisfiesCriteria(searchCriteria)
select r;
Write a parser class that parses searchCritera and stores it in parsed format. (for e.g. you can convert it into postfix notation). This is quite easy and you can use standard algorithm for this. For your purpose OR, AND will be operators and PwC etc. will be operands. Parenthesis will get removed as part of parsing.
Now simply invoke this parser from with in your extension method and then evaluate the postfix expression you get. This again can be done through standard stack based evaluation. Infact it would be better if you parse the criteria once and then only evaluate in where. While evaluating you need to replace the operands with r.Value.Contains
It seems like a good scenario for http://scriptcs.net/

Categories