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 8226851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:55:13+00:00 2026-06-07T15:55:13+00:00

Is there a tool or library that converts object serialized into YAML or XML

  • 0

Is there a tool or library that converts object serialized into YAML or XML file into Java code creating that object?

From technical point of view, I see no difficulties here. Both Yaml and XStream (or other similar tools) needs to find Java class name via reflection, find constructor, invoke it, then find setters and invoke them. Reflection invokes can be replaced by Java code generation that is doing the same. It requires a bit work, however.

Does such tool exist? Or maybe someone is developing it and want to share it with public?

P.S. Any library with ability to convert given Java object into Java code would fullfill my needs.

clarification

I expect the tool to convert example YAML:

--- !pl.example.Contact
name: Nathan Sweet
age: 28

Or XML:

<contact>
  <name>Nathan Sweet</name>
  <age>28</age>
</contact>

into the code:

pl.example.Contact contact = new pl.example.Contact();
contact.setName("Nathan Sweet");
contact.setAge(28);
  • 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-07T15:55:16+00:00Added an answer on June 7, 2026 at 3:55 pm

    I’ve asked about converting YAML or XML into Java code, but that what I really needed was to generate Java code that would create given Java object. Reading existing YAML can be done by one of many Java YAML libraries.

    However, I wasn’t able to find any library that would create Java code from given Java object. Maybe I was searching for wrong keywords, maybe I’ve finished my search too early, but I haven’t found anything.

    I have written that tool myself. The code is quite primitive and probably will not be enough in many cases (it has no support for abstract classes and interfaces etc.), but was good enough for the task I wanted to accomplish. So I post the source code of my solution as the answer to my question:

    import java.beans.PropertyDescriptor;
    import java.lang.reflect.InvocationTargetException;
    import java.util.*;
    
    import org.apache.commons.beanutils.PropertyUtils;
    import org.apache.commons.lang.StringEscapeUtils;
    
    public class BeanCodeGenerator {
    
        private class Context {
            private int index;
            private StringBuilder sb = new StringBuilder();
            private String br = "\n\t\t";
        }
    
        public String generateBeanMethod(Object bean, String methodName) {
            Context ctx = new Context();
            Class type = _getBeanClass(bean);
            ctx.sb.append("\tpublic ").append(type.getCanonicalName()).append(" ").append(methodName)
                .append("() {").append(ctx.br);
    
            String vname = "null";
            try {
                vname = _valueStr(bean, type, ctx);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            ctx.sb.append("return ").append(vname).append(";\n");
            ctx.sb.append("\t}\n");
            return ctx.sb.toString();
        }
    
        private String _outputBean(Object bean, Context ctx) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // output block of code creating bean
            String vname = "v" + ctx.index;
            String cname = bean.getClass().getCanonicalName();
            ctx.sb.append(cname).append(" ").append(vname).append(" = new ").append(cname).append("();").append(ctx.br);
            PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(bean);
            for (PropertyDescriptor prop : props) {
                if (prop.getReadMethod() == null || prop.getWriteMethod() == null)
                    continue; // skip such 'properties'
                Object value = prop.getReadMethod().invoke(bean);
                if (value == null)
                    continue;
                String valueStr = _valueStr(value, prop.getReadMethod().getReturnType(), ctx);
                if (valueStr != null) {
                    ctx.sb.append(vname).append(".").append(prop.getWriteMethod().getName()).append("(")
                        .append(valueStr).append(");").append(ctx.br);
                }
            }
            return vname;
        }
    
        private Class _getBeanClass(Object bean) {
            if (bean == null)
                return null;
           if (_isCollection(bean)) {
               if (bean instanceof List)
                   return ArrayList.class;
               if (bean instanceof Set)
                   return HashSet.class;
               if (bean instanceof Map)
                   return HashMap.class;
           }
           return bean.getClass();
        }
    
        private String _valueStr(Object value, Class type, Context ctx) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            String valueStr = _outputValue(value, type);
            if (valueStr == null) {
                if (_isBean(value)) {
                    ctx.index++;
                    valueStr = _outputBean(value, ctx);
                } else if (_isCollection(value)) {
                    ctx.index++;
                    valueStr = _outputCollection(value, ctx);
                }
            }
            if (valueStr == null)
                valueStr = "null";
            return valueStr;
        }
    
        private String _outputCollection(Object collection, Context ctx) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            String vname = "v" + ctx.index;
            if (collection instanceof List) {
                ctx.sb.append("java.util.ArrayList ").append(vname).append(" = new java.util.ArrayList();").append(ctx.br);
                List list = (List) collection;
                for (Object value : list) {
                    if (value == null) {
                        ctx.sb.append(vname).append(".add(null);").append(ctx.br);
                        continue;
                    }
                    String valueStr = _valueStr(value, value.getClass(), ctx);;
                    ctx.sb.append(vname).append(".add(").append(valueStr).append(");").append(ctx.br);
                }
            } else if (collection instanceof Set) {
                ctx.sb.append("java.util.HashSet ").append(vname).append(" = new java.util.HashSet();").append(ctx.br);
                Set set = (Set) collection;
                for (Object value : set) {
                    if (value == null) {
                        ctx.sb.append(vname).append(".add(null);").append(ctx.br);
                        continue;
                    }
                    String valueStr = _valueStr(value, value.getClass(), ctx);;
                    ctx.sb.append(vname).append(".add(").append(valueStr).append(");").append(ctx.br);
                }
            } else if (collection instanceof Map) {
                ctx.sb.append("java.util.HashMap ").append(vname).append(" = new java.util.HashMap();").append(ctx.br);
                Map map = (Map) collection;
                for (Object item : map.entrySet()) {
                    Map.Entry entry = (Map.Entry) item;
                    String valueStr = "null";
                    String keyStr = "null";
                    if (entry.getKey() != null) {
                        keyStr = _valueStr(entry.getKey(), entry.getKey().getClass(), ctx);
                    }
                    if (entry.getValue() != null) {
                        valueStr = _valueStr(entry.getValue(), entry.getValue().getClass(), ctx);
                    }
                    ctx.sb.append(vname).append(".put(").append(keyStr).append(", ").append(valueStr).append(");").append(ctx.br);
                }
            }
            return vname;
        }
    
        private boolean _isBean(Object value) throws SecurityException {
            if (value == null)
                return false;
            if (_isCollection(value))
                return false;
            Class type = value.getClass();
            try {
                type.getConstructor();
                return true;
            } catch (NoSuchMethodException e) {
                return false;
            }
        }
    
        private boolean _isCollection(Object value) {
            if (value == null)
                return false;
            Class type = value.getClass();
            return List.class.isAssignableFrom(type) || Set.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type); 
        }
    
        // output simple value, returns null for non-singleliners
        private String _outputValue(Object value, Class type) {
            if (value == null)
                return null;
            Class vtype = value.getClass();
            if (type == long.class) {
                return value.toString() + "L";
            }
            if (type == float.class) {
                return value.toString() + "F";
            }
            if (type == short.class) {
                return "(short) " + value.toString() + "";
            }
            if (type.isPrimitive()) {
                return value.toString();
            }
            if (String.class.isAssignableFrom(type)) {
                return '"' + StringEscapeUtils.escapeJava(value.toString()) + '"';
            }
            if (Integer.class == type || Double.class == type || Boolean.class == type) {
                return type.getName() + ".valueOf(" + value.toString() + ")";
            }
            if (Long.class == type) {
                return type.getName() + ".valueOf(" + value.toString() + "L)";
            }
            if (Short.class == type) {
                return type.getName() + ".valueOf((short)" + value.toString() + ")";
            }
            if (Float.class == type) {
                return type.getName() + ".valueOf(" + value.toString() + "F)";
            }
            if (Date.class.isAssignableFrom(type)) {
                Date date = (Date) value;
                return "new java.util.Date(" + date.getTime() + "L)";
            }
            return null;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any java library that is similar to unix's command file ? ie:
Is there any tool/library that calculate percent of condition/decision coverage of python code. I
I'm wondering if there is any tool/library that would fill Java objects with data
I am looking for a good tool or library that takes a C/Cpp/header file
I have to code a Java program that will receive messages from network and
Is there any tool that can directly test if a library is made for
Is there some *nix tool or perl/php library that will let you easily create
Is there a tool for dumping the contents of a library or JAR file
I'm looking for a Java library that would allow me to marshal XML to
Is there a tool to generate stubs for me, or a library to interact

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.