How to use TFDJSONDataSets or similar in XE4? - json

Explanation
Today i have spotted the Delphi XE6 Sample called FireDACJSONReflect and i would to use the way it does the communication between the datasnap server and the client, i want to do it on Mine application using TFDMemTableand TFDJSONDataSets(not really needed, but if there is another way using another components i would like to see it).
** Mine application is a delphi-xe4 server and a delphi-xe6 client **
Problem
By the way, my application does use Delphi XE4 instead of Delphi XE6 and want to know what to do to communicate between the datasnap server and the client the same way that FireDACJSONReflect does but i see that i cant have TFDJSONDataSets on my project uses list.
Can i use TFDJSONDataSets on Delphi XE4?
Can i use TFDMemTable without using TFDJSONDataSets to populate it with data?
Some code
I have that lines of code running ok on delphi xe6 and i want to do the same in xe4:
function TDSSMetodoFinanceiro.getCotacaoLista : TFDJSONDataSets;
var
FDQCotacaoLista : TADQuery;
begin
Result := TFDJSONDataSets.create;
TFDJSONDataSetsWriter.listAdd(result, FDQCotacaoLista);
end;
And the question: Why i want to result a TFDJSONDataSet?
Its because i have a TFDMemTable on my delphi xe6 client application that i want to .appendData and it needs a TFDJSONDataset to do it(maybe another way to append data do it is useful).
Any help?

TFDJSONDatasets is, I believe, introduced in XE6, so you cannot use this in earlier versions like XE4. However, I think the job can be done with a FDMemtable instead.
Check out this tutorial, it was written for XE5 but I think this must work in XE4 too.
It starts of from some other example using a Clientdataset, but is then reworked to use a Firedac Memory table.
I should say, give it a try and see if it works out for you.

Related

Is there a way to force the UWP RichEditBox use only UTF encoding when the user types?

I am trying to convert the contents of a UWP RichEditBox to HTML.
For that purpose, I've tried using the RtfPipe library (https://github.com/erdomke/RtfPipe). From the looks of it, this library has a problem on UWP, due to the fact that not all encodings are defined on that target framework. (This is the error you get, if you are interested: Encoding.GetEncoding can't work in UWP app, but the accepted answer seems not to be the best option on all platforms - I haven't even managed to make the suggested fix compile, so it might not be valid anymore)
Now, as a way of avoiding this from happening, I am wondering whether there is a way to force the control to always use one of the UWP-defined UTF-variants for encoding the data when the user types his text.
Because, now, when I type into it, I get things like that:
{\rtf1\fbidis\ansi\ansicpg1253\deff0\nouicompat\deflang1032{
....
\pard\tx720\cf1\f0\fs23\lang1033
...that make the library throw exceptions.
I guess, if I manage to make it not use ASCII code pages, things will be great.
After taking a look at the control properties though, I do not see something I could use. Is there any way to achieve this?
This is the error you get, if you are interested: Encoding.GetEncoding can't work in UWP app
As you described, there is an inner error thrown when using this package with UWP app. System.ArgumentException: 'Windows-1252' is not a supported encoding name, by testing on my side, which is thrown by the code line public static readonly Encoding AnsiEncoding = Encoding.GetEncoding("Windows-1252"); of RtfSpec.cs when UpdateEncoding.
It seems like Windows-1252 may not be supported in UWP from the error details,also see this similar thread. You could use UTF instead as you want, for example, have a change on the library with following then it will work (testing demo here).
public static readonly Encoding AnsiEncoding = Encoding.UTF8;
I haven't even managed to make the suggested fix compile, so it might not be valid anymore
Encoding.RegisterProvider method should be work, but it only support UWP or .NET Framework 4.6, it does't support the Portable Class Library. The RtfPipe library you mentioned is Portable Class Library, so that you cannot use Encoding.RegisterProvider. Encoding.GetEncoding method supports Portable Class Library, details please check the version information of the two classed.
I guess, if I manage to make it not use ASCII code pages
RTF itself uses the ANSI, PC-8, Macintosh, or IBM PC character set to control the representation and formatting of a document, you may not able to change that. Consider to update the library to resolve the issue for UWP.

Why do I get a slew of question marks when I parse a string with SuperObject?

I'm trying to parse this string using SuperObject in Delphi 7.
procedure TForm1.btn1Click(Sender: TObject);
var
obj: ISuperObject;
fw:string;
begin
fw:= '{"type":"normal","info":{"Name":"frank","Number":"01","Age":"21","registered":"Yes","Support":"Expired"}}';
obj := TSuperObject.ParseString(PWideChar(fw), false);
mmo1.lines.Add(obj.AsJSon(true,false));
end;
But result in the memo is like this:
"????????????????????????????????????????????????????}"
What am I doing wrong?
fw is a string which in Delphi 7 is 8 bit ANSI encoded. The cast to PWideChar is thus incorrect. It will treat the 8 bit text as if it were UTF-16 encoded.
If the function you are calling really does receive PWideChar then you need to convert to UTF-16 first. For example like this:
PWideChar(WideString(fw))
You also report a separate problem that arises in the super object code. Specifically this line of code:
h := h*129 + ord(k[i]) + $9e370001;
raises an overflow error.
That happens because your project has the overflow checking option enabled (good practice to do so), but the super object code has been written under the assumption that the option is disabled. This is really a flaw in the super object code. You can solve it by disabling overflow checking in the super object code by adding {$OVERFLOWCHECKS OFF}. Ideally this would be disabled very locally for just the code that intentionally overflows. However, unless you fully understand the code it may just be easier to stuff {$OVERFLOWCHECKS OFF} at the top of the unit and move on.
Now, I'm looking at the very latest super object code and right at the top of the unit is {$OVERFLOWCHECKS OFF}. So I wonder if you are perhaps using an out of date version of the code. Pull the latest version from the repo.
I had the same problem using SuperObjects1.2.4 in delphi 7. As many people said, the solution is: to get the latest version. So, to get the lastest SuperObjects version from repository do:
1. Install git (http://git-scm.com/)
2. Right click in some Folder, choose 'Git bash' and paste the following
git clone https://code.google.com/r/steve-superobject/
Done!

When to encode as HTML in Grails

I often see Grails sample code where the programmer has called a method called encodeAsHTML(). I figure I should probably use this in my Grails applications (for security reasons, I assume?), but I was wondering when I should use this method. What objects/properties/etc. are candidates for the encodeAsHTML() method?
Thank you!
Use encodeAsHTML() (or encodeAsJavaScript, etc) for everything that you've got from user. For every string that could be modified by user (got from input form, from request parameter, from external API call, etc)
See also:
https://en.wikipedia.org/wiki/Cross-site_scripting
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet
I am not sure when this was introduced to Grails, but if in Config.groovy you set grails.views.default.codec="html" then encodeAsHTML() is called whenever you use ${} in GSPs.
Source: http://alwaysthecritic.typepad.com/atc/2010/06/grails-gsp-html-escaping-confusion.html

getWindowHandle function doesn't exist for driver in Selenium

I need to implement switch from one window to another in IE. However, element driver doesn't support getWindowHandle function.
I assume it might be just configuration problem or settings, though I don't know how to fix it.
Please, any suggestions.
I'm working with c# - Visual Studio
You haven't said which language bindings you're using, but based on a comment you posted, it looks like you're using C#. The method names are slightly different for each language binding. From this answer:
The object, method, and property names in the .NET language bindings
do not exactly correspond to those in the Java bindings. One of the
principles of the project is that each language binding should "feel
natural" to those comfortable coding in that language.
So you have to do a little translation if you're trying to copy-paste Java code. In this case, you want the combination of the WindowHandles property (to look for the new window handle) and the CurrentWindowHandle property of the driver. You can find full API documentation for the .NET bindings at the project's Google code site.
I am going to make wild guess:
Try to initialize your driver like this:
WebDriver driver = new FirefoxDriver(); //assume you use firefox
The interface WebDriver supports that method. Do not forget to store the handle somewhere ;)
String myWindow = driver.getWindowHandle();
BTW that method should return you actual window If you need all windows you probably should use getWindowHandles() method
If this does not work, please provide more info:
what error exactly are you getting?
How do you initialize WebDriver?
What version of selenium are you using?|
What type of driver are you using?

"Calling 'Read' when the data reader is closed is not a valid operation.", but only on one of the Include paths

I'm using the Entity Framework in C# with a MySQL back-end. Here's the problem section of the code:
using (var entities = new myEntities()) {
Parties = new ObservableCollection<t_party>(
entities.SalesParties
.Include("SalesReps")
.Include("InventoryReservation")
.Include("InventoryReservation.InventoryAssignment")
.Include("InventoryReservation.InventoryAssignment.Inventory")
.ToList()
);
}
When the code runs, I get an error: "Calling 'Read' when the data reader is closed is not a valid operation." The interesting part is that if I remove the .Include("SalesReps") it works just fine. SalesReps and InventoryReservation are both 0..1 multiplicity from the SalesParty end and * from the other end.
I'm using the Entity Framework 4.1 with the "MySQL Connector Net 6.3.7" library. I tried 6.4.x initially, but ran into some other problems between it and the Entity Framework and had to roll back.
The truly mystifying thing is that I recently switched laptops, and it was running fine on the old one! The old one was running Windows 7 on a 32-bit processor, the new one is 64-bit. Not sure if that would affect things by using different libraries, but it's the only other variable I can think of.
I also got the same issue and found that once the using block has been executed, the entities variable will be disposed. So if you try to use it again outside of the block, you will get this error. To solve this problem create entities variable without any using block and then try to run the code.