Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:33:19+00:00 2026-06-01T07:33:19+00:00

Say I have a REST API in java, and it supports responses that are

  • 0

Say I have a REST API in java, and it supports responses that are either JSON or XML. The responses contain the same data, but the form is not identical. For example, in json I might have:

{
    "persons": [
        {
            "name":"Bob",
            "age":24,
            "hometown":"New York"
        }
    ]
}

Whereas in XML it looks like this:

<persons>
    <person name="bob" age="24">
        <hometown>New York</hometown>
    </person>
</persons>

Which is to say that some values are attributes on person, and others are child elements. In Java, using JAXB and Jackson, it’s easy to hide such differences with annotations on the model objects, for example:

public class Person {
    @XmlAttribute
    String name;

    @XmlAttribute
    Integer age;

    @XmlElement 
    String hometown; 
}

JAXB reads the annotations, and Jackson uses the field names to figure out what to do. So with a single model it’s easy to support multiple output formats.

So my question is, how to do the same thing in clojure. I know that there is clj-json which can easily convert clojure maps and vectors to json (using jackson if I’m not mistaken). And I know there is both clojure.xml.emit and clojure.contrib.xml.prxml that can deserialize maps & vectors to XML. But unless I’m mistaken, I don’t think these two would work together very well.

Because prxml expects xml nodes to be expressed as vectors, and xml attributes to be expressed as a map, fundamentally different than the working of clj-json, where vectors represent arrays, and maps represent objects. And clojure.core.emit expects a map in the form {:tag :person :attrs {:name "Bob" :age 24} :content ...} which is again completely different than what clj-json wants.

The only thing I can think of is to format the data structures for prxml in my code, and then write a function that transforms the data structure to what clj-json wants when the response type is JSON. But that seems kind of lame. I would prefer if there was a pair of JSON and XML libraries that were compatible in the way that JAXB and Jackson are.

Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:33:20+00:00Added an answer on June 1, 2026 at 7:33 am

    A lot depends on how you choose to represent models in your code.

    Let’s assume you use records. Here’s a contrived example of how you might “annotate” a record and provide serializers for XML and JSON.

    ;; Depends on cheshire and data.xml
    (ns user
      (:require [cheshire.core :as json]
                [clojure.data.xml :as xml]))
    
    (defrecord Person [name age hometown])
    (defrecord Animal [name sound])
    
    (def xml-attrs {Person [:name :age]
                    Animal [:name]})
    
    (defn record->xml-data [rec]
      (let [tag (-> rec class .getSimpleName .toLowerCase keyword)
            attrs (select-keys rec (xml-attrs (class rec)))
            content (for [[k v] rec
                          :when (not (contains? attrs k))]
                      (xml/element k nil (str v)))]
        (apply xml/element tag attrs content)))
    
    (defn record->xml [rec]
      (xml/emit-str (record->xml-data rec)))
    
    (defn record->json [rec]
      (json/generate-string rec))
    

    Usage:

    > (def bob (Person. "Bob" 24 "New York"))
    #'user/bob
    
    > (println (record->xml bob))
    <?xml version="1.0" encoding="UTF-8"?><person age="24" name="Bob"><hometown>New York</hometown></person>
    nil
    
    > (println (record->json bob))
    {"name":"Bob","age":24,"hometown":"New York"}
    nil
    
    > (println (record->xml (Animal. "Fido" "Bark!")))
    <?xml version="1.0" encoding="UTF-8"?><animal name="Fido"><sound>Bark!</sound></animal>
    nil
    

    A macro could be created to define a record and its XML attributes in a singe statement. E.g.,

    (defrecord-xml Person [^:xml-attr name ^:xml-attr age hometown])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Let's say I have a REST method to update a record. That would obviously
Let's say I have built a REST service for making notes that looks something
Lets say I have a Rest API which can be accessed via e.g.: mypage.com/v1/users/1234
We have REST API that we want only our domain has access to and
Let's say you have a complex resource in a REST API. You have several
Say I have a function foo: (defun foo (x y &rest args) ...) And
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
I need to make a rest api call to a website say xyx.com. It
I have the following REST API: POST /users/martin/notify/... The API is suppose to notify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.