Error compressing javascript file

Jan 27, 2011 at 1:12 PM

There is an error in a Library, when there a json property and a value of this property is a decimal the char "." chancge in ","

example:

 

mjsnon = {
	value : 1.3
}

 

result

mjsnon={value:1,3}

anyone can help me? thanks

Coordinator
Jan 28, 2011 at 1:13 AM

Hi Giopao86.

When ever I see a ZERO being replaced by a COMMA, the first thing I think of is a Region Setting / localisation issue. So, what is the region your computer is set to? Italian? Spanish? IS it anything but US-English, UK-English, Whatever-English .. ??

Jan 28, 2011 at 6:41 AM
Edited Jan 28, 2011 at 6:55 AM

my region setting is italian, but my site can be published on other regional setting site, can i set an universal region setting and how?

this is a code that I've used

string script = File.ReadAllText(context.Request.PhysicalPath);            
File.WriteAllText(filePath, JavaScriptCompressor.Compress(script));

How I can change this code?

thanks for the help :)

p.s. I've realize that this behavior is on all the decimal number (not only json value), and the problem can be this

Coordinator
Jan 28, 2011 at 1:39 PM

Ah -- i knew i was onto something :)

U'll need to set the localisation for the running thread, this YUICompressor code will excute under. I've exposed a number of static constructors for you OR you could manually create the instance and set the property OR use one of the many instance constructors instead.

 

So you're using the static constructor. Lets see which is the smallest static constructor with the Thread's CultureInfo defined in it ...

public static string Compress(string javaScript,
                                      bool isVerboseLogging,
                                      bool isObfuscateJavascript,
                                      bool preserveAllSemicolons,
                                      bool disableOptimizations,
                                      int lineBreakPosition,
                                      Encoding encoding,
                                      CultureInfo threadCulture) { .. }

so try this ... (untested, pseudo code written on the fly, in this reply post)

string script = File.ReadAllText(context.Request.PhysicalPath);            
File.WriteAllText(filePath, JavaScriptCompressor.Compress(script, true, true, false, false, -1, Encoding.Default, new CultureInfo( "en-UK", false ) ));

Something like that ... where we have to define the culture info as english-something ... i prefer UK english over US .. but that will preserve the 'dot' and not replace it with a 'comma', because of the current thread culture is set to Italian by default, on your machine.

Does this help?

 

 

Jan 29, 2011 at 1:11 PM

ok, Monday i return to wark and i will try, other question the library is in gpl2 license ,can I use that in a commercial web application?

Coordinator
Jan 30, 2011 at 10:35 PM

I'm currently reviewing the license of this library. I want it to be free and usable in any project (commercial or non-commercial) .. but i think the current license doesn't allow this. I'll be updating this VERY soon :)

 

Keep me posted on your testing, when u get back to work on Monday. Good Luck!

Coordinator
Feb 1, 2011 at 2:32 AM

The current license has now been changed to New BSD .. which I believe means u can use it in a commercial (closed-system) product but just give some credit in you source code, somewhere.

Feb 1, 2011 at 7:28 AM

your help was really helpful, only a correction the culture info en-UK not exist, I've used en-GB and works perfectly

thanks for Everything

Coordinator
Feb 1, 2011 at 9:16 PM

pleasure! and doh! i always get uk/gb mixed up :( at least my documentation has it right :P

GL with it and enjoy!

Oct 5, 2011 at 9:06 PM

tanks, was very helpfull

Jan 31, 2012 at 3:57 PM

Sorry to resurrect/animate this one, but I just downloaded 1.7.0 and am having similar issues, meaning that I cannot compress json objects with decimal values.

Yahoo.Yui.Compressor.JavaScriptCompressor.Compress("var stuff = {foo:0.9, faa:3};", false, false, false, false, 200, Encoding.UTF8, CultureInfo.InvariantCulture);

This results in -> "var apa={foo:0,9,faa:3};"

double comma and a broken json. I have tried to get en-GB culture as well as others that should use comma as decimal separator but to no avail. What am I missing?

Please please please throw me a bone :)

Coordinator
Jan 31, 2012 at 10:37 PM

I'll throw ya a bone :)

So 99.99999% of the time, this is because the machine that is running the compression work has a default regional setting to something european .. like France. Now, what is interesting is that you said that you've tried to force the culture to en-GB. Hmm. ok. Lets try and repoduce your error.

So i started up Visual Studio 2010 -> new console app. NuGet package install YUICompressor.NET v.1.7.0.0 and added in some test code.

Here's the test code on pasite.org for you copy/paste on your own machine and try out.

For me, it works great :)

Ok, so the next thing here is for you to confirm that you're correctly setting the culture. When you run the YUICompressor, you should get some output stuff .. which confirms what settings have been 'recognised'. One of these settings is the CultureInfo. Any chance you could give us the YUICompressor output/dump so we can see what settings are being used?

Feb 1, 2012 at 9:05 AM

Cheers purekrome,

Actually it DOES work. It seems that it is enough to set the Thread.CurrentThread.CultureInfo to Invariant to output invariant. And the parameter I pass to compress is to read Invariant. And then I had the files saved in Western European somthing codepage and not UTF8, meaning that there was a total messup of special characters.

So if anyone else have these issues: UTF8 on all the files, Threads culture to invariant and compress parameter to invariant. PROFIT!

Thanks again for your quick response! 

Coordinator
Feb 1, 2012 at 9:31 AM

Are you changing this on your thread prior to calling YUICompressor, as well as passing InvariantCulture in here:

Yahoo.Yui.Compressor.JavaScriptCompressor.Compress("var stuff = {foo:0.9, faa:3};", false, false, false, false, 200, Encoding.UTF8, CultureInfo.InvariantCulture);

I don't think this should be necessary, but need PK to confirm...

Coordinator
Feb 1, 2012 at 10:48 AM

I think we've also identified a bug in our code, also @AllNamesRTaken.

I've supplied a patch so I might update NuGet soon with this bugfix. I'm going to see if @FreeRanger can confirm my fix.

Coordinator
Feb 1, 2012 at 12:33 PM

Looks good there PK.  I added two tests (I will check them in shortly):

 

        [TestMethod]
        public void If_CultureInfo_Is_Supplied_Then_The_Output_Respects_It_Irrespective_Of_The_Current_Thread_Culture()
        {
            // Arrange
            var currentThreadCulture = Thread.CurrentThread.CurrentCulture;
            var currentThreadUiCulture = Thread.CurrentThread.CurrentUICulture;
            const string source = "var stuff = {foo:0.9, faa:3};";
            const string  expected = "var stuff={foo:0.9,faa:3};";

            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("it-IT");
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("it-IT");

                // Act
                var actual = JavaScriptCompressor.Compress(source, false, false, false, false, 200, Encoding.UTF8, CultureInfo.InvariantCulture);

                // Assert.
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentThreadCulture;
                Thread.CurrentThread.CurrentUICulture = currentThreadUiCulture;
            }
        }

        [TestMethod]
        public void If_CultureInfo_Is_Not_Supplied_Then_The_Output_Respects_The_Current_Thread_Culture()
        {
            // Arrange
            var currentThreadCulture = Thread.CurrentThread.CurrentCulture;
            var currentThreadUiCulture = Thread.CurrentThread.CurrentUICulture;
            const string source = "var stuff = {foo:0.9, faa:3};";
            const string expected = "var stuff={foo:0,9,faa:3};";

            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("it-IT");
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("it-IT");

                // Act
                var actual = JavaScriptCompressor.Compress(source, false, false, false, false, 200);

                // Assert.
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = currentThreadCulture;
                Thread.CurrentThread.CurrentUICulture = currentThreadUiCulture;
            }
        }

 

The first one fails with the 1.7.0.0 code, but they both pass with your new patch applied - w00t!