Rectangle 27 19

As Nishant already pointed out in the comments, you've found a bug here.

This bug initially was reported in 2004 (>9 years ago!?!!) and this bug seems not to be fixed very soon (or ever). I tested it on several Eclipse version, even on Eclipse Juno SR2, and the bug is still there.

by the way it's still not fixed if anyone was wondering

java - How to get carriage return without line feed effect in Eclipse ...

java eclipse console carriage-return linefeed
Rectangle 27 2

Also the empty string is a distinct value and should be returned already, but likely you're simply not observing it. Changing your query to

for $ex in distinct-values(//products/product/@group)
return <value>{ $ex }</value>

you will get a result like

<value>2</value>
<value>1</value>
<value/>

(while <value/> is equivalent to <value></value>, and "contains" the empty string).

If you want to match both a single product for empty groups and all products without a group, you can for example split apart the query in two parts: filtering for products with groups, and then adding those without.

(
  (: return first product for each product group :)
  for $group in distinct-values(//products/product/@group)
  (: unless group is empty :)
  where $group
  return (//product[@group eq $group])[1],
  (: add products without any group :)
  //product[@group eq '']
)

Thats pretty nice, I updated my question what my result expectation looks like. Maybe you can help me getting both products with empty groups there too?

I already observed the edit and extended the answer.

does that mean that first every product with a group comes and afterwards every product with no group? how to order it if we have an attribute named order e.g. <product id="25" order="1"> ?

Wrap in another flwor expression and user order by.

xml - How to get all ungrouped items with distinct-values() in XQuery?...

xml xpath xquery
Rectangle 27 262

A backslash in a jQuery selector escapes the next character. But you need two of them because backslash is also the escape character for JavaScript strings. The first backslash escapes the second one, giving you one actual backslash in your string - which then escapes the next character for jQuery.

$(function() {
  $.getJSON("/Location/GetCountryList", null, function(data) {
    $("#Address\\.Country").fillSelect(data);
  });
  $("#Address\\.Country").change(function() {
    $.getJSON("/Location/GetRegionsForCountry", { country: $(this).val() }, function(data) {
      $("#Address\\.State").fillSelect(data);
    });
  });
});

I wonder what the rationalization is for requiring the developer to do this, it seems like a simple set of heuristics within jQuery would solve this problem, since there isn't anything wrong with ID's with periods in them...

If you didn't have to escape, how would you query an element with ID Address and class State (granted that if you know the ID you aren't adding much by specifying the class, but it should still be valid, I'd think)?

For that reason, in CSS itself, you must escape a dot in an ID, too. All jQuery is doing is requiring you to follow the rules set out by CSS.

You could also select by name instead of ID. For example, $('[name="Address.Country"]')

javascript - How do I get jQuery to select elements with a . (period) ...

javascript jquery jquery-selectors
Rectangle 27 1398

I just add the following in App_Start / WebApiConfig.cs class in my MVC Web API project.

config.Formatters.JsonFormatter.SupportedMediaTypes
    .Add(new MediaTypeHeaderValue("text/html") );

That makes sure you get json on most queries, but you can get xml when you send text/xml.

Content-Type
application/json
NameSpace
System.Net.Http.Headers

This is a surprisingly overlooked answer, and although the original question wasn't totally clear, this directly makes JSON the default response for a web browser (which sends Accept: text/html). Good job.

+1 Far and away the best answer. I imagine there are a ton of ppl who opt to completely remove XML just because they don't see JSON in the browser.

Note that the response's Content-Type header will still be text/html.

This is horrible. The response content type header should be application/json. This "solution" makes it text/html.

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 1398

I just add the following in App_Start / WebApiConfig.cs class in my MVC Web API project.

config.Formatters.JsonFormatter.SupportedMediaTypes
    .Add(new MediaTypeHeaderValue("text/html") );

That makes sure you get json on most queries, but you can get xml when you send text/xml.

Content-Type
application/json
NameSpace
System.Net.Http.Headers

This is a surprisingly overlooked answer, and although the original question wasn't totally clear, this directly makes JSON the default response for a web browser (which sends Accept: text/html). Good job.

+1 Far and away the best answer. I imagine there are a ton of ppl who opt to completely remove XML just because they don't see JSON in the browser.

Note that the response's Content-Type header will still be text/html.

This is horrible. The response content type header should be application/json. This "solution" makes it text/html.

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 1397

I just add the following in App_Start / WebApiConfig.cs class in my MVC Web API project.

config.Formatters.JsonFormatter.SupportedMediaTypes
    .Add(new MediaTypeHeaderValue("text/html") );

That makes sure you get json on most queries, but you can get xml when you send text/xml.

Content-Type
application/json
NameSpace
System.Net.Http.Headers

This is a surprisingly overlooked answer, and although the original question wasn't totally clear, this directly makes JSON the default response for a web browser (which sends Accept: text/html). Good job.

+1 Far and away the best answer. I imagine there are a ton of ppl who opt to completely remove XML just because they don't see JSON in the browser.

Note that the response's Content-Type header will still be text/html.

This is horrible. The response content type header should be application/json. This "solution" makes it text/html.

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 3

It finally seems to be fixed in Mars!

Version: Mars Release (4.5.0)
Build id: 20150621-1200
Used Java: jre1.8.0_60
public class Main {

    public static void main(String[] args) {
        System.out.print("something\r");
        System.out.print(" any\r");
    }
}
anything

weird ask: how can I make it to treat "\n" as "\r\n" again? (version Oxygen)

java - How to get carriage return without line feed effect in Eclipse ...

java eclipse console carriage-return linefeed
Rectangle 27 459

If you do this in the WebApiConfig you will get JSON by default, but it will still allow you to return XML if you pass text/xml as the request Accept header

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

If you are not using the MVC project type and therefore did not have this class to begin with, see this answer for details on how to incorporate it.

Just to note, the original behaviour is correct. Chrome requests application/xml with a priority of 0.9 and */* with a priority of 0.8. By removing application/xml you remove the ability for the Web API to return XML if the client requests that specifically. e.g. if you send "Accept: application/xml" you will still receive JSON.

Is it me, or is the first sentence incorrect? The code appears to totally remove XML, not simply change the default.

@NickG: a solution that is overlooked here and IMHO is a much better option (keeping application/xml) is the solution proposed by Felipe Leusin lower on this page. Using config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

So, how do we do it via web config so we get json by default and XML if requested?

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 459

If you do this in the WebApiConfig you will get JSON by default, but it will still allow you to return XML if you pass text/xml as the request Accept header

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

If you are not using the MVC project type and therefore did not have this class to begin with, see this answer for details on how to incorporate it.

Just to note, the original behaviour is correct. Chrome requests application/xml with a priority of 0.9 and */* with a priority of 0.8. By removing application/xml you remove the ability for the Web API to return XML if the client requests that specifically. e.g. if you send "Accept: application/xml" you will still receive JSON.

Is it me, or is the first sentence incorrect? The code appears to totally remove XML, not simply change the default.

@NickG: a solution that is overlooked here and IMHO is a much better option (keeping application/xml) is the solution proposed by Felipe Leusin lower on this page. Using config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

So, how do we do it via web config so we get json by default and XML if requested?

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 459

If you do this in the WebApiConfig you will get JSON by default, but it will still allow you to return XML if you pass text/xml as the request Accept header

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

If you are not using the MVC project type and therefore did not have this class to begin with, see this answer for details on how to incorporate it.

Just to note, the original behaviour is correct. Chrome requests application/xml with a priority of 0.9 and */* with a priority of 0.8. By removing application/xml you remove the ability for the Web API to return XML if the client requests that specifically. e.g. if you send "Accept: application/xml" you will still receive JSON.

Is it me, or is the first sentence incorrect? The code appears to totally remove XML, not simply change the default.

@NickG: a solution that is overlooked here and IMHO is a much better option (keeping application/xml) is the solution proposed by Felipe Leusin lower on this page. Using config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

So, how do we do it via web config so we get json by default and XML if requested?

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 241

I like Felipe Leusin's approach best - make sure browsers get JSON without compromising content negotiation from clients that actually want XML. The only missing piece for me was that the response headers still contained content-type: text/html. Why was that a problem? Because I use the JSON Formatter Chrome extension, which inspects content-type, and I don't get the pretty formatting I'm used to. I fixed that with a simple custom formatter that accepts text/html requests and returns application/json responses:

public class BrowserJsonFormatter : JsonMediaTypeFormatter
{
    public BrowserJsonFormatter() {
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        this.SerializerSettings.Formatting = Formatting.Indented;
    }

    public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) {
        base.SetDefaultContentHeaders(type, headers, mediaType);
        headers.ContentType = new MediaTypeHeaderValue("application/json");
    }
}
config.Formatters.Add(new BrowserJsonFormatter());
this.SerializerSettings.Formatting = Formatting.Indented;

why would you want it to pretty print over the wire?

Perfect. I was having issues with Felipe's solution returning a Content-Type of text/html and this fixed it (and made the response correct!) I combined your answer with suhair's answer so that I get JSON sent to the browser by default but have the option of adding "format=xml" or "format=json" to the querystring to select a specific formatter. Thanks!

Isn't @dmit77 's Answer better (more concise) than this one?

@eddiegroves you dont want pretty-print over the wire. You want the server to send the least amount of bits over the wire (ie: no spaces). Then you want the browser to format it nicely, with addons and such. Javascript needs to parse the JSON usually, why make it slower by introducing unnecessary formatting

using System.Net.Http.Formatting
using Newtonsoft.Json

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 241

I like Felipe Leusin's approach best - make sure browsers get JSON without compromising content negotiation from clients that actually want XML. The only missing piece for me was that the response headers still contained content-type: text/html. Why was that a problem? Because I use the JSON Formatter Chrome extension, which inspects content-type, and I don't get the pretty formatting I'm used to. I fixed that with a simple custom formatter that accepts text/html requests and returns application/json responses:

public class BrowserJsonFormatter : JsonMediaTypeFormatter
{
    public BrowserJsonFormatter() {
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        this.SerializerSettings.Formatting = Formatting.Indented;
    }

    public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) {
        base.SetDefaultContentHeaders(type, headers, mediaType);
        headers.ContentType = new MediaTypeHeaderValue("application/json");
    }
}
config.Formatters.Add(new BrowserJsonFormatter());
this.SerializerSettings.Formatting = Formatting.Indented;

why would you want it to pretty print over the wire?

Isn't @dmit77 's Answer better (more concise) than this one?

@eddiegroves you dont want pretty-print over the wire. You want the server to send the least amount of bits over the wire (ie: no spaces). Then you want the browser to format it nicely, with addons and such. Javascript needs to parse the JSON usually, why make it slower by introducing unnecessary formatting

using System.Net.Http.Formatting
using Newtonsoft.Json

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 241

I like Felipe Leusin's approach best - make sure browsers get JSON without compromising content negotiation from clients that actually want XML. The only missing piece for me was that the response headers still contained content-type: text/html. Why was that a problem? Because I use the JSON Formatter Chrome extension, which inspects content-type, and I don't get the pretty formatting I'm used to. I fixed that with a simple custom formatter that accepts text/html requests and returns application/json responses:

public class BrowserJsonFormatter : JsonMediaTypeFormatter
{
    public BrowserJsonFormatter() {
        this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        this.SerializerSettings.Formatting = Formatting.Indented;
    }

    public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) {
        base.SetDefaultContentHeaders(type, headers, mediaType);
        headers.ContentType = new MediaTypeHeaderValue("application/json");
    }
}
config.Formatters.Add(new BrowserJsonFormatter());
this.SerializerSettings.Formatting = Formatting.Indented;

why would you want it to pretty print over the wire?

Isn't @dmit77 's Answer better (more concise) than this one?

@eddiegroves you dont want pretty-print over the wire. You want the server to send the least amount of bits over the wire (ie: no spaces). Then you want the browser to format it nicely, with addons and such. Javascript needs to parse the JSON usually, why make it slower by introducing unnecessary formatting

using System.Net.Http.Formatting
using Newtonsoft.Json

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 94

Is XmlFormatter new in MVC4?

In MVC5, this can be done by replacing config with GlobalConfiguration.Configuration

For a project that must support JSON only and under no circumstance can be allowed to emit XML this is by far the best option.

That's terrible. -- This will always return JSON no matter what, even if the client specifically asks for XML in the Content-Type header.

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 94

Is XmlFormatter new in MVC4?

In MVC5, this can be done by replacing config with GlobalConfiguration.Configuration

For a project that must support JSON only and under no circumstance can be allowed to emit XML this is by far the best option.

That's terrible. -- This will always return JSON no matter what, even if the client specifically asks for XML in the Content-Type header.

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 94

Is XmlFormatter new in MVC4?

In MVC5, this can be done by replacing config with GlobalConfiguration.Configuration

For a project that must support JSON only and under no circumstance can be allowed to emit XML this is by far the best option.

That's terrible. -- This will always return JSON no matter what, even if the client specifically asks for XML in the Content-Type header.

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 163

MVC4 Quick Tip #3Removing the XML Formatter from ASP.Net Web API

Global.asax
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    BundleTable.Bundles.RegisterTemplateBundles();
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}

Works - much nicer having JSON be the default instead of XML.

but can you still return xml then?

I tested it, and you can't. So this is removing XML support.. Ye be warned, dear google people

If you have a look at my answer below, this will let xml still be returned if you want to but lets the site respond with JSON to the browser

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 163

MVC4 Quick Tip #3Removing the XML Formatter from ASP.Net Web API

Global.asax
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    BundleTable.Bundles.RegisterTemplateBundles();
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}

Works - much nicer having JSON be the default instead of XML.

but can you still return xml then?

I tested it, and you can't. So this is removing XML support.. Ye be warned, dear google people

If you have a look at my answer below, this will let xml still be returned if you want to but lets the site respond with JSON to the browser

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api
Rectangle 27 163

MVC4 Quick Tip #3Removing the XML Formatter from ASP.Net Web API

Global.asax
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    BundleTable.Bundles.RegisterTemplateBundles();
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}

Works - much nicer having JSON be the default instead of XML.

but can you still return xml then?

I tested it, and you can't. So this is removing XML support.. Ye be warned, dear google people

If you have a look at my answer below, this will let xml still be returned if you want to but lets the site respond with JSON to the browser

How do I get ASP.NET Web API to return JSON instead of XML using Chrom...

json google-chrome asp.net-web-api